如何在PHP中访问stdClass的成员 [英] How to access a member of an stdClass in PHP
本文介绍了如何在PHP中访问stdClass的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个解码的JSON对象,由于出现错误,我无法访问该元素.
I have a JSON object that I decoded, and I can't access the element because it throws an error.
stdClass Object
(
[error] => InvalidRegistration
)
echo $device_response->error; // gives an error
我该如何访问属性?
下面是整个响应数组:
stdClass Object
(
[multicast_id] => 5.3301797222004E+18
[success] => 1
[failure] => 3
[canonical_ids] => 0
[results] => Array
(
[0] => stdClass Object
(
[message_id] => 0:1388910534147789%25796c83f9fd7ecd
)
[1] => stdClass Object
(
[error] => InvalidRegistration
)
[2] => stdClass Object
(
[error] => InvalidRegistration
)
[3] => stdClass Object
(
[error] => InvalidRegistration
)
)
)
下面是我的访问方式:
foreach ($responseBody->results as $device_response) {
echo $device_response->error;
}
推荐答案
$obj = new stdClass();
$obj->foo = "var";
echo $obj->foo;
这篇关于如何在PHP中访问stdClass的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文