doucong3048 2015-08-02 22:43
浏览 27
已采纳

如何从API响应数组中访问数据

I am trying to loop over data from a an API response that has returned a PHP array but I am having problems looping over the data.

I am using the Tumblr API and would like to loop over the blog_names that have been returned by the array.

Here is what I have tried so far:

API response var_dump($info)

object(stdClass)[4]
  public 'meta' => 
    object(stdClass)[5]
      public 'status' => int 200
      public 'msg' => string 'OK' (length=2)
  public 'response' => 
    array (size=20)
      0 => 
        object(stdClass)[6]
          public 'blog_name' => string 'zolloc' (length=6)
          public 'id' => int 124153530139
          public 'post_url' => string 'http://zolloc.com/post/124153530139/david-hart-nyfwm-davidhartnyc-cfda-cadillac' (length=79)
          public 'slug' => string 'david-hart-nyfwm-davidhartnyc-cfda-cadillac' (length=43)
          public 'type' => string 'photo' (length=5)
          public 'date' => string '2015-07-15 13:34:10 GMT' (length=23)
          public 'timestamp' => int 1436967250
          public 'state' => string 'published' (length=9)
          public 'format' => string 'html' (length=4)
          public 'reblog_key' => string 'QhlVD4ex' (length=8)
          public 'tags' => 
            array (size=15)
              ...
          public 'short_url' => string 'http://tmblr.co/Zh19vx1pe88SR' (length=29)
          public 'recommended_source' => null
          public 'featured_in_tag' => 
            array (size=3)
              ...
          public 'featured_timestamp' => int 1437012340
          public 'highlighted' => 
            array (size=0)
              ...
          public 'note_count' => int 800
          public 'source_url' => string 'http://zolloc.com' (length=17)
          public 'source_title' => string 'zolloc.com' (length=10)
          public 'caption' => string 'david hart / nyfwm davidhartnyc cfda cadillac' (length=45)
          public 'reblog' => 
            object(stdClass)[7]
              ...
          public 'trail' => 
            array (size=1)
              ...
          public 'link_url' => string 'http://zolloc.com' (length=17)
          public 'image_permalink' => string 'http://zolloc.com/image/124153530139' (length=36)
          public 'photos' => 
            array (size=1)
              ...

Trying to loop over the data to get each blog_name

foreach($info['response'] as $item) {

    echo $item['blog_name'];
}

Can anyone tell me what I have missed out in order to access the data in the array please?

  • 写回答

1条回答 默认 最新

  • dpvmtdu364462 2015-08-02 22:57
    关注

    Both $info and elements stored in response array are objects, so you need to use object syntax to access the date. Replace your loop with:

    foreach($info->response as $item) {
      echo $item->blog_name;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证