为什么我不能在PHP中访问此数组的第二级?

Recently, with help from the Stack Oveflow community, I learned how to work with JSON data that I got from the Google API. So, I thought I had a grasp on how to do roughly the same kind of action with JSON data from Blogger, again using the Google API.

However, it's been driving me crazy. I can't seem to get a handle on how to get at the right level of the array that I need.

What I am trying to do is get a selection of blog posts and then cherry pick out information and display it. Should be straight forward.

Right now my blog has only three test entries. I want to see if I can loop through the array I create from the JSON to display data. If I use this code:

$recentBlog = json_decode(file_get_contents("https://www.googleapis.com/blogger/v3/blogs/xxxxxxxxxxxxxxxxxxxx/posts?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));

foreach ($recentBlog as $item)
{
    var_dump($item[0]);
}

... then I get data the first item only from the array. This makes sense to me. I looped through the array, but on each call, I only asked for the first item at the 0 position.

So I thought I would do this:

$recentBlog = json_decode(file_get_contents("https://www.googleapis.com/blogger/v3/blogs/xxxxxxxxxxxxxxxxxxxx/posts?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));

$count = 0;
foreach ($recentBlog as $item)
{
    var_dump($item[$count]);
    $count = $count + 1;
}

... but I get the second and third item only. I do not get the first item anymore.

What is wrong with my code?

If it helps, this is Google's documentation about the format of the JSON that I get back.

You probably have a wrong parenthesis in your JSON decoding call:

$recentBlog = json_decode(file_get_contents("https://[...]"), true);

Note that I moved one parenthesis directly behind the file_get_contents() function. This way json_decode() will return you an iterable array.

You get the response like below. If you want to loop the blog entries you have to loop $recentBlog['items'] in foreach.

foreach ($recentBlog['items'] as $item)
{
    var_dump($item);
}

Response:

{
      "kind": "blogger#postList",
      "nextPageToken": "CgkIChiAkceVjiYQ0b2SAQ",
      "prevPageToken": "CgkIChDBwrK3mCYQ0b2SAQ",
      "items": [
        {
          "kind": "blogger#post",
          "id": "7706273476706534553",
          "blog": {
            "id": "2399953"
          },
          "published": "2011-08-01T19:58:00.000Z",
          "updated": "2011-08-01T19:58:51.947Z",
          "url": "http://buzz.blogger.com/2011/08/latest-updates-august-1st.html",
          "selfLink": "https://www.googleapis.com/blogger/v3/blogs/2399953/posts/7706273476706534553",
          "title": "Latest updates, August 1st",
          "content": "elided for readability",
          "author": {
            "id": "401465483996",
            "displayName": "Brett Wiltshire",
            "url": "http://www.blogger.com/profile/01430672582309320414",
            "image": {
              "url": "http://4.bp.blogspot.com/_YA50adQ-7vQ/S1gfR_6ufpI/AAAAAAAAAAk/1ErJGgRWZDg/S45/brett.png"
             }
          },
          "replies": {
            "totalItems": "0",
            "selfLink": "https://www.googleapis.com/blogger/v3/blogs/2399953/posts/7706273476706534553/comments"
          }
        },
        {
          "kind": "blogger#post",
          "id": "6069922188027612413",
          elided for readability
        }
      ]
    }