dongmu3457 2018-03-21 08:08 采纳率: 0%
浏览 69
已采纳

解析JSON PHP中的子文档数组

I got the following JSON string in PHP:

{  
   "status":"200",
   "message":"Saved picklist found",
   "data":{  
      "_id":{  
         "$oid":"5aab871dbcdcab3ab0005cd3"
      },
      "username":"admin",
      "list_count":"3",
      "list":[  
         {  
            "id":"1",
            "data":"AUTOGENERATED1",
            "x":"33",
            "y":"33"
         },
         {  
            "id":"2",
            "data":"AUTOGENERATED2",
            "x":"22",
            "y":"22"
         },
         {  
            "id":"3",
            "data":"AUTO",
            "x":"33",
            "y":"33"
         }
      ]
   }
}

that I decode to an array using the following:

json_decode($response, true);

I would like to loop through the array and extract for example username and list_count. I tried the following code for that:

foreach ($response['data'] as $resp) {
     echo $resp['list_count'];
}

which does not work. Any suggestions on how to extract username and list_count?

I would also like to loop through the list array and get the values for each object within that array, any suggestions on how that can be done?

  • 写回答

3条回答 默认 最新

  • dousou2911 2018-03-21 08:19
    关注

    You can use this code.

    $dd = json_decode($json,true);
    echo $dd["data"]["username"]."<br/>"; //get value of username
    
    //Loop through list to get all values
    foreach($dd["data"]["list"] as $key=>$value){
    echo $value['id']."<br/>";
    echo $value['data']."<br/>";
    echo $value['x']."<br/>";
    echo $value['y']."<br/>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?