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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题