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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理