如何访问一个stdClass的成员在PHP与一个开始@ [英] How to access a member of an stdClass in PHP that starts with an @
本文介绍了如何访问一个stdClass的成员在PHP与一个开始@的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些JSON对象,我去codeD和属性之一首先是一个@因为它抛出一个错误,我不能用PHP访问的元素。
I have some json object that I decoded, and one of the attributes starts with an "@" and I can't access the element with php because it throws an error.
[offers] => stdClass Object
(
[@attributes] => stdClass Object
(
[id] => levaka0B8a
)
)
我怎么会去访问属性?
How would I go about accessing attributes?
推荐答案
您可以用字符串访问:
echo $obj->{'@attributes'}->id; // levaka0B8a
或变量:
$name = '@attributes';
echo $obj->$name->id;
有关变量是如何定义和使用的详细信息,请参阅以下文档:
For more information on how variables are defined and used, see the following docs:
- Variable Basics - Useful for learning what can be accessed as a variable without needing to use strings.
- Variable Variables - How we used the variable to act as the name for another variable. This can be dangerous so tread carefully
这篇关于如何访问一个stdClass的成员在PHP与一个开始@的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文