如何访问php花括号对象属性
可能重复:如何访问php花括号对象属性,php,object,properties,curly-braces,curly-brackets,Php,Object,Properties,Curly Braces,Curly Brackets,可能重复: print\r($myObj)给出以下结果: stdClass Object ( [4021450] => stdClass Object ( [property1] => ooo [property2] => xxx ) [3971601] => stdClass Object ( [property1] => 123 [property2] =&
print\r($myObj)
给出以下结果:
stdClass Object
(
[4021450] => stdClass Object
(
[property1] => ooo
[property2] => xxx
)
[3971601] => stdClass Object
(
[property1] => 123
[property2] => 356
)
)
如何使用带变量的大括号语法访问子对象
我试过:
$myObj->'3971601'; // Parse error: syntax error
$myObj->{'3971601'}; // Works
$id = 3971601; $myObj->{$id}; // Notice: Trying to get property of non-object
$id = 3971601; $myObj->{''.$id}; // Notice: Trying to get property of non-object
$arr = (array)$myObj; $arr[3971601]; // Notice: Undefined offset: 3971601
$arr = (array)$myObj; $arr['3971601']; // Notice: Undefined index: 3971601
您应该能够完全省略大括号:
$myObj->$id
。然而,你最后的4个例子表明有些地方不对劲。似乎在某个地方,$myObj
被设置为null
或其他一些非对象值