我有一个从Facebook API接收到的解码json数组:
$Myposts = json_decode($response->getBody());
当我打印$ Myposts时:
var_dump($Myposts);
它给出了:
object(stdClass)[16]
public 'data' =>
array (size=24)
0 =>
object(stdClass)[12]
public 'message' => string 'a mesagge..' (length=65)
public 'created_time' => string '2016-09-18T16:41:10+0000' (length=24)
public 'id' => string '111110037' (length=35)
1 =>
object(stdClass)[28]
public 'message' => string 'How brave !' (length=11)
public 'story' => string 'XXX shared Le XX video.' (length=59)
public 'created_time' => string '2016-09-18T13:37:33+0000' (length=24)
public 'id' => string '102172976' (length=35)
23 =>
object(stdClass)[50]
public 'message' => string '...a message..' (length=259)
public 'story' => string 'Bi added 3 new photos.' (length=33)
public 'created_time' => string '2015-12-11T20:54:21+0000' (length=24)
public 'id' => string '102191588' (length=35)
public 'paging' =>
object(stdClass)[51]
public 'previous' => string 'https://graph.facebook.com/v2.7/XXXX&__paging_token=YYYY&__previous=1' (length=372)
public 'next' => string 'https://graph.facebook.com/v2.7/XXX/feed?access_token=DDDD&limit=25&until=XXX&__paging_token=XXXX' (length=362)
我是php的新手,我不知道如何处理此输出,我想遍历所有消息并输出每条消息的created_time。
有任何想法吗 ?
编辑:我试过了:echo $myPosts['data'][message];
从用PHP解析JSON文件,但是我有:“未定义的索引:消息”。这就是为什么我发布了一个新问题。
第二个参数json_decode
将结果转换为关联数组:
$myPosts = json_decode($response->getBody(), true);
foreach ($myPosts['data'] as $post) {
var_dump($post['message']);
}
在不使用第二个参数的情况下,解码返回对象:
$myPosts = json_decode($response->getBody());
foreach ($myPosts->data as $post) {
var_dump($post->message);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句