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