在PHP中访问使用simplexml_load_file创建的多维数组对象返回null?
问题描述:
我使用PHP simplexml_load_file
函数创建了一个RSS对象$rssObject
,目标是获得值为[href]
。在PHP中访问使用simplexml_load_file创建的多维数组对象返回null?
var_dump($rssObject)
返回如下:
Array
(
[0] => SimpleXMLElement Object
(
[link] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => https://www.example.com
)
)
我也没能成功拿到[href]
内容使用这个符号,其不返回null
$rssObject[0]->link[0]->{'@attributes'}['href'];
知道为什么? 任何帮助,将不胜感激!
答
SimpleXML中,属性是使用访问阵列符号:
$xml = simplexml_load_file();
$url = $xml[0]->link[0]['href'];
见 “Example #5 Using attributes” PHP手册。
这是为什么被低估?请参阅答案,当var_dump输出一个误导输出时,它证明了这个问题! – Mohammad
是的,这是一个非常普遍的混乱,我们应该真正有一个解释它的规范答案。 – IMSoP