getting php variable from an object in which one of its properties has $ sign

天涯浪子 提交于 2020-01-15 01:57:08

问题


so I acquired a json object from an API and json decoded it...

but then the returned object is of the following:

stdClass Object
(
    [id] => stdClass Object
        (
            [$t] => some string
        )
)

And as you can see there is a property with a $ sign in it

but when I do $object->id->$t <--that returns error since it thinks $t is a variable

how would I go about fetching that variable with $ sign?


回答1:


Encapsulate it as a single-quoted string:

echo $object->id->{'$t'};

Note that you cannot use double quotes for the same reason you cannot use $object->id->$t: PHP will attempt to interpolate $t. However, you could use double quotes if you escape the dollar sign:

echo $obj->id->{"\$t"};

You can see it working in this simple demo.



来源:https://stackoverflow.com/questions/11871872/getting-php-variable-from-an-object-in-which-one-of-its-properties-has-sign

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