如何在PHP中访问stdClass的成员

时间:2014-01-05 08:31:31

标签: php json stdclass

我有一个我解码的JSON对象,我无法访问该元素,因为它会抛出错误。

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;

    }

1 个答案:

答案 0 :(得分:0)

$obj = new stdClass();
$obj->foo = "var";
echo $obj->foo;