douchun3680 2013-09-09 08:48
浏览 23
已采纳

为什么我不能在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.

  • 写回答

2条回答 默认 最新

  • douyan8070 2013-09-09 09:08
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c