dongxuan2015 2016-04-03 04:03
浏览 8
已采纳

无法从json_decode访问值

I have a json object from a curl get request.

// Curl Stuff
$resp = curl_exec($curl);
{"food": {"id":585897,"foodGroup":"meats","calories":1109,"foodTier":30}}

I saved it in a variable with json_decode

$data = json_decode($resp, TRUE);

I've tried accessing the data in a couple ways but I get no response

echo $data[0][1];
echo $data[0]['id'];

Also if someone can point me in the right direction of looping through this data I would appreciate that.

  • 写回答

1条回答 默认 最新

  • douqin6785 2016-04-03 04:10
    关注

    With the true parameter in your json_decode, you have associative array, so it must be:

    echo $data['food']['id'];
    

    Get the id by numeric:

    $da = array();
    foreach($data as $key=>$val){
      $da[$key] = array_values($val);
    }
    print_r($da);
    
    echo $da['food'][0];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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