简体   繁体   中英

JSON return 2 array with 1 element

I have this function:

public function findFavoriteMerchant($userId) {
  // I grab all the merchant_id from favoriteMerchant where user_id = $userId
  $q = $this->_db->prepare("SELECT * FROM favoriteMerchant WHERE user_id = ?");
  $q->bindParam(1, $userId, PDO::PARAM_INT);


        if ($q->execute()){

        $result = $q->fetchAll(PDO::FETCH_ASSOC);
        $i = -1;

              foreach ($result as $row) {

              $merchant = $this->_db->prepare("SELECT * FROM merchant WHERE merchant_id = ?");
              $merchant->bindParam(1, $row["merchant_id"], PDO::PARAM_INT);

                   if ($merchant->execute()){

                       $i++;

                       $merchantArray[$i] = $merchant->fetchAll(PDO::FETCH_ASSOC);
                    }
               }        
               return $merchantArray;
         }
} 

that return this :

[
  [ 
    {
        "merchant_id": "17",
        "business_name": "Nails Spa",
        "merchant_first_name": "Jonh ",
        "merchant_last_name": "Lampard",
        "merchant_email": "email@me.com",
        "merchant_address": "123 lampard street",
        "merchant_country": "United States",
        "merchant_city": "San Francisco ",
        "merchant_state": "Dubai",
        "merchant_postalCode": "92",
        "merchant_telephone": "043251653",
        "merchant_industry": "Beauty",
        "merchant_type": "Spa",
        "merchant_service": "other",
        "merchant_lat": "37.804512",
        "merchant_long": "-122.432335"
    }
],
[
    {
        "merchant_id": "19",
        "business_name": "Massage Spa",
        "merchant_first_name": "carl",
        "merchant_last_name": "smith",
        "merchant_email": "email@me.com",
        "merchant_address": "",
        "merchant_country": "United States",
        "merchant_city": "San Francisco ",
        "merchant_state": "Dubai",
        "merchant_postalCode": "92",
        "merchant_telephone": "043278439",
        "merchant_industry": "Beauty",
        "merchant_type": "Spa",
        "merchant_service": "other",
        "merchant_lat": "25.037189",
        "merchant_long": "55.251812"
    }
  ]
]

but I would like to get this:

[
    {
        "merchant_id": "17",
        "business_name": "Nails Spa",
        "merchant_first_name": "Jonh ",
        "merchant_last_name": "Lampard",
        "merchant_email": "email@me.com", 
      // and so on....

****UPDATE****

    $merchant =  new Merchant();
    echo json_encode($merchant->findFavoriteMerchant($userId),JSON_PRETTY_PRINT);

print r returns:

    Array
(
    [0] => Array
        (
            [merchant_id] => 17
            [business_name] => Massage Spa
            [merchant_first_name] => Jonh 
            [merchant_last_name] => Lampard
            [merchant_email] => email@me.com
            [merchant_address] => 123 lampard street
            [merchant_country] => United States
            [merchant_city] => San Francisco 
            [merchant_state] => Dubai
            [merchant_postalCode] => 92
            [merchant_telephone] => 043251653
            [merchant_industry] => Beauty
            [merchant_type] => Spa
            [merchant_service] => other
            [merchant_lat] => 37.804512
            [merchant_long] => -122.432335
            [merchant_userName] => Aviation 
            [password] => cfc10a708f01fe27750e3be246ca1daa
        )

)
Array
(
    [0] => Array
        (
            [merchant_id] => 19
            [business_name] => Angsana Spa
            [merchant_first_name] => carl
            [merchant_last_name] => smith
            [merchant_email] => email@me.com
            [merchant_address] => 
            [merchant_country] => United States
            [merchant_city] => San Francisco 
            [merchant_state] => Dubai
            [merchant_postalCode] => 92
            [merchant_telephone] => 043278439
            [merchant_industry] => Beauty
            [merchant_type] => Spa
            [merchant_service] => other
            [merchant_lat] => 25.037189
            [merchant_long] => 55.251812

        )

)

I tried several approaches but I don't get it

$merchant->fetchAll() returns an array. you added an array to result array, two times. take a first item or use foreach loop.

You need to take the individual elements from the return of fetchAll since it returns an array.

Use a simple foreach to populate your final array.
Everything else stays the same, return $merchantArray , json_encode it then print it out.

$results = $merchant->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $result){
    $merchantArray[$i] = $result;
}

You could either extract the first row from the result of the second query

$merchantArray[$i] = $merchant->fetchAll(PDO::FETCH_ASSOC)[0];

Or you could use a JOIN to get all of the information at once

public function findFavoriteMerchant($userId) {
  // grab all merchant information from merchant where the merchant_id matches from favoriteMerchant where user_id = $userId
  $q = $this->_db->prepare("
    SELECT merch.*
    FROM favoriteMerchant AS fav
    INNER JOIN merchant merch ON fav.merchant_id = merch.merchant_id
    WHERE user_id = ?");
  $q->bindParam(1, $userId, PDO::PARAM_INT);

    if ($q->execute()){
      $merchantArray = $q->fetchAll(PDO::FETCH_ASSOC);
      return $merchantArray;
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM