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

报告相同问题?