如何访问数组中的数组?(How to access array in array?)

编程入门 行业动态 更新时间:2024-10-24 12:21:44
如何访问数组中的数组?(How to access array in array?)

如何从Facebook Graph Api访问帖子数据,如[message]和[created_time]?

这是来自Facebook Graph Api的JSON的print_r

Array ( [posts] => Array ( [data] => Array ( [0] => Array ( [message] => random message. [created_time] => 2016-11-12T07:26:23+0000 [id] => id786456786 ) [1] => Array ( [message] => random message. [created_time] => 2016-11-11T04:30:02+0000 [id] => id123456768 ) ) [paging] => Array ( [previous] => https://graph.facebook.com/v2.8/pageid/posts?limit=2&since=1478935583&access_token=MY ACCESS TOKEN .... ) ) [id] => id )

我的php代码:

$data = file_get_contents("https://graph.facebook.com/v2.8facebookpageid?fields=posts.limit(2)&access_token=MY_ACCESS_TOKEN"); $decoded = json_decode($data,true); print_r($decoded);

任何帮助感谢!

how can i access posts data, like [message] and [created_time] from Facebook Graph Api?

Here is the print_r of JSON , which I'm getting from Facebook Graph Api

Array ( [posts] => Array ( [data] => Array ( [0] => Array ( [message] => random message. [created_time] => 2016-11-12T07:26:23+0000 [id] => id786456786 ) [1] => Array ( [message] => random message. [created_time] => 2016-11-11T04:30:02+0000 [id] => id123456768 ) ) [paging] => Array ( [previous] => https://graph.facebook.com/v2.8/pageid/posts?limit=2&since=1478935583&access_token=MY ACCESS TOKEN .... ) ) [id] => id )

My php code:

$data = file_get_contents("https://graph.facebook.com/v2.8facebookpageid?fields=posts.limit(2)&access_token=MY_ACCESS_TOKEN"); $decoded = json_decode($data,true); print_r($decoded);

Any help appreciated!

最满意答案

你可以简单地遵循数组的关键:

$decoded = json_decode($data, true); // Retrieve the first message. $message = $decoded['posts']['data'][0]['message'] // Retrieve the second message. $message = $decoded['posts']['data'][1]['message']

更好的是,你可以循环遍历$decoded['posts']['data']如下所示:

$decoded = json_decode($data, true); foreach ($decoded['posts']['data'] as $item) { echo "Message: {$item['message']}\n"; echo "Created Time: {$item['created_time']}\n"; }

在此处阅读有关PHP Array的更多信息

希望这个帮助!

You can just simply follow the array's key:

$decoded = json_decode($data, true); // Retrieve the first message. $message = $decoded['posts']['data'][0]['message'] // Retrieve the second message. $message = $decoded['posts']['data'][1]['message']

Even better, you can loop through the $decoded['posts']['data'] like so:

$decoded = json_decode($data, true); foreach ($decoded['posts']['data'] as $item) { echo "Message: {$item['message']}\n"; echo "Created Time: {$item['created_time']}\n"; }

Read more about PHP Array here.

Hope this help!

更多推荐

本文发布于:2023-08-07 08:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   组中   array   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!