从对象的其中一个属性具有$符号的对象获取php变量 [英] getting php variable from an object in which one of its properties has $ sign

查看:297
本文介绍了从对象的其中一个属性具有$符号的对象获取php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我从API获取了一个json对象,并用json对其进行了解码...

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

但是返回的对象如下:

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

如您所见,其中有一个带有$符号的属性

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

但是当我执行$object->id->$t<时-返回错误,因为它认为$ t是变量

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?

推荐答案

将其封装为单引号字符串:

Encapsulate it as a single-quoted string:

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

请注意,出于与不能使用$object->id->$t相同的原因,不能使用双引号:PHP将尝试对$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.

这篇关于从对象的其中一个属性具有$符号的对象获取php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆