douchunji1885 2017-01-04 17:41
浏览 61
已采纳

PHP cURL API响应处理

I have a question regarding the handling of a response from an API using the cURL function from PHP. If I access the API via cURL I get the following response:

{"status":"ok","meta":{"count":1},"data":{"502268596":{"clan_id":500022074}}}

I transformed this code (named $json) using:

$jsondecoded = json_decode($json,true);

If I var_dump the associative array I now got I receive the following:

array(3) { ["status"]=> string(2) "ok" ["meta"]=> array(1) { ["count"]=> int(1) } ["data"]=> array(1) { [502268596]=> array(1) { ["clan_id"]=> int(500022074) } } }

My Question is: How do I access the field "clan_id"? I'm absolutely lost, because I don't have a real understanding of how arrays work and how I cycle through them.

  • 写回答

1条回答 默认 最新

  • dopgv00024 2017-01-04 17:49
    关注

    Your response data is in array format. In this specific case, you can access clan id as follows:

    echo $jsondecoded['data'][502268596]['clan_id'];
    

    If you would rather work with objects, you can do so this way:

    $jsondecoded = json_decode($json);
    echo $jsondecoded->data->{502268596}->clan_id;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用