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

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)