如何访问数组中的数组?(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!
更多推荐
发布评论