2017-01-04 17:41
浏览 57


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:


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.

图片转代码服务由CSDN问答提供 功能建议

我有一个关于使用PHP的cURL函数处理来自API的响应的问题。 如果我访问 API通过cURL得到以下响应:

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

我使用以下方法转换了此代码(名为$ json): < pre> $ jsondecoded = json_decode($ json,true);


  array(3){[“status”] =&gt;  string(2)“ok”[“meta”] =&gt;  array(1){[“count”] =&gt;  int(1)} [“data”] =&gt;  array(1){[502268596] =&gt;  array(1){[“clan_id”] =&gt;  int(500022074)}}} 

我的问题是:如何访问字段“clan_id”? 我完全迷失了,因为我对阵列的工作方式以及循环方式没有真正的了解。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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;
    解决 无用
    打赏 举报

相关推荐 更多相似问题