How to access a member of an stdClass in PHP

此生再无相见时 提交于 2019-12-02 12:36:56

问题


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

How would I go about accessing attributes?

Below is whole array of response:

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
                )

        )

)

Below is how I am accessing it:

foreach ($responseBody->results as $device_response) {

        echo $device_response->error;

    }

回答1:


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


来源:https://stackoverflow.com/questions/20931600/how-to-access-a-member-of-an-stdclass-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!