编程语言
首页 > 编程语言> > 如何在PHP中访问stdClass的成员

如何在PHP中访问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;

    }

解决方法:

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

标签:stdclass,php,json
来源: https://codeday.me/bug/20191013/1910363.html