如何访问密钥名称中包含哈希字符的对象


How to access an object with a hash character in the key name?

如何访问$key名称中有#的PHP对象?

示例:

[image] => stdClass Object 
       ( 
       [#text] => http://userserve-ak.last.fm/serve/_/85003/Red+Hot+Chili+Peppers.jpg 
       [name] => original 
       [width] => 937 
       [height] => 1276 
       )

我想访问#text属性,但$image->#text不起作用,因为PHP将#解释为注释的开头。

我该怎么做?

您可以尝试使用:

$image->{'#text'}

可能是这样的:(不确定)

$image->{"#text"}

一种方法是使用可变密钥名称:

$keyname = '#text';
$value = $image->$keyname;