dousou2897 2016-01-16 13:02
浏览 50
已采纳

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

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

  • 写回答

3条回答 默认 最新

  • duanjiao4763 2016-01-16 13:17
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化