JSON返回2个数组,包含1个元素

时间:2016-01-16 13:02:44

标签: php json

我有这个功能:

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;
         }
} 

返回此:

[
  [ 
    {
        "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"
    }
  ]
]

但我希望得到这个:

[
    {
        "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返回:

    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

        )

)

我尝试了几种方法,但我没有得到它

3 个答案:

答案 0 :(得分:1)

$ merchant-> fetchAll()返回一个数组。你向结果数组添加了一个数组,两次。采取第一项或使用foreach循环。

答案 1 :(得分:1)

您需要从fetchAll的返回中获取单个元素,因为它返回一个数组。

使用简单的foreach填充最终数组 其他所有内容保持不变,返回$merchantArrayjson_encode然后将其打印出来。

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

答案 2 :(得分:1)

您可以从第二个查询的结果中提取第一行

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

或者你可以使用JOIN一次获取所有信息

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;
    }
}