PHP访问simplexml_load_file创建的多维数组对象返回null?

问题描述 投票:0回答:1

我有一个使用 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'];

这是为什么?

php arrays object rss simplexml
1个回答
2
投票

在 SimpleXML 中,使用数组表示法访问属性:

$xml = simplexml_load_file();
$url = $xml[0]->link[0]['href'];

参见 PHP 手册中的“Example #5 Using attributes”。

© www.soinside.com 2019 - 2024. All rights reserved.