我有一个json一个对象数组,我想从中读取[message]我试着用foreach循环的帮助但是它给出了我的错误
未定义的属性:第56行的C:\ xampp \ htdocs \ fb \ select-action.php中的stdClass :: $ name
这是第56行
foreach($data->data as $obj)
{
echo $obj->message.'<br>';
}
包含对象json数组的$ data变量显示了这种类型的结果
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[comments] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[created_time] => 2017-09-19T09:17:24+0000
[from] => stdClass Object
(
[name] => Hasni_Collections786
[id] => 581985938822055
)
[message] => :-p ;)
[id] => 737576403263007_737576629929651
)
[1] => stdClass Object
(
[created_time] => 2017-09-24T12:54:28+0000
[from] => stdClass Object
(
[name] => Hasni_Collections786
[id] => 581985938822055
)
[message] => :-p
[id] => 737576403263007_740798849607429
)
[2] => stdClass Object
(
[created_time] => 2017-09-24T12:54:31+0000
[from] => stdClass Object
(
[name] => Hasni_Collections786
[id] => 581985938822055
)
[message] => :-p
[id] => 737576403263007_740798896274091
)
)
[paging] => stdClass Object
(
[cursors] => stdClass Object
(
[before] => WTI5dGJXVnVkRjlqZAFhKemIzSTZAOek0zTlRjMk5qSTVPVEk1TmpVeE9qRTFNRFU0TVRJMk5EUT0ZD
[after] => WTI5dGJXVnVkRjlqZAFhKemIzSTZAOelF3TnprNE9EazJNamMwTURreE9qRTFNRFl5TlRjMk56RT0ZD
)
)
)
[id] => 581985938822055_737576403263007
)
)
[paging] => stdClass Object
(
[cursors] => stdClass Object
(
[before] => Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UTFPREU1T0RVNU16ZAzRNakl3TlRVNkxUZAzNPRE0xTnpJNU9UQTVOekExTVRVNE56SVBER0ZA3YVY5emRHOXllVjlwWkE4ZAk5UZA3hPVGcxT1RNNE9ESXlNRFUxWHpjek56VTNOalF3TXpJMk16QXdOdzhFZAEdsdFpRWlp3T0I4QVE9PQZDZD
[after] => Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UTFPREU1T0RVNU16ZAzRNakl3TlRVNkxUZAzNPRE0xTnpJNU9UQTVOekExTVRVNE56SVBER0ZA3YVY5emRHOXllVjlwWkE4ZAk5UZA3hPVGcxT1RNNE9ESXlNRFUxWHpjek56VTNOalF3TXpJMk16QXdOdzhFZAEdsdFpRWlp3T0I4QVE9PQZDZD
)
)
)
答案 0 :(得分:2)
您的响应似乎是一个混合的Array / stdObject。如果您的给定输出来自$ data,那么您的foreach必须看起来像这样:
foreach ($data->data[0]->comments->data as $obj) {
echo $obj->message.'<br>';
}
有没有json_decode()?似乎某些部件被铸造成阵列或类似的东西。考虑使用json_decode($ json,true),这会将整个结果作为一个数组给出,而不会出现这种奇怪的混淆。