访问simpleXML对象时为什么输出为空?

时间:2016-06-21 11:33:21

标签: php xml

我的simpleXMLElement输出

SimpleXMLElement Object
(
[guid] => 2898580
[title] => England's doomed inevitability? 
[description] => 
[link] => http://www.espnfc.com/blog/the-match/60/post/2895340/roy-hodgson-rings-the-changes-but-england-struggle-again
[pubDate] => Jun 20, 2016 04:58 PM PDT
[enclosure] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [length] => 150
                [url] => http://a.espncdn.com/combiner/i/?img=/photo/2016/0620/r94714_1296x729_16-9.jpg&w=100&h=80&scale=crop&site=espnfc
                [type] => image/jpeg
            )

    )

[category] => null
) 

PHP 当我在其中打印时,在foreach循环的帮助下

 echo "Img_Url:".$img_url = $item->enclosure->attributes['url'] ;  

它会在那里显示空白但我写这个

echo "Img_Url:".$img_url = $item->enclosure->@attributes['url'] ; 

它给出了语法错误。

所以我怎么打印这个。

1 个答案:

答案 0 :(得分:2)

要访问属性,只需将SimpleXMLElement对象视为数组即可。不要在对象的内部结构中挖掘。

 echo "Img_Url: " . (string) $item->enclosure['url'];

请参阅http://php.net/manual/en/simplexml.examples-basic.php