我有一个使用 PHP
$rssObject
函数创建的 RSS 对象 simplexml_load_file
,目标是获取 [href]
的值。
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'];
参见 PHP 手册中的“Example #5 Using attributes”。