0

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

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.