回显SimpleXMLElement对象

时间:2016-11-24 05:20:10

标签: php

我有一个看起来像这样的数组。

Array
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [title] => Active Fire Incident Page
            [link] => http://www.ci.austin.tx.us/fact/default.cfm
            [description] => This page provides information on active Austin/Travis County Fire incidents.  Data is updated every 3 minutes.

            [lastBuildDate] => Wed, 23 Nov 2016 10:59:38 PM GMT
            [copyright] => 2016 City of Austin. All rights reserved.
            [language] => en-us
            [item] => SimpleXMLElement Object
                (
                    [title] => E ST ELMO RD/SHERATON AVE
                    [link] => http://www.ci.austin.tx.us/fact/default.cfm
                    [description] => AFD - 4700 S Congress Ave - BOX -Structure Fire - Wed, 23 Nov 2016 10:51 PM 
                    [pubdate] => Wed, 23 Nov 2016 10:51 PM 
                )

        )

)

我如何回应数组的最后部分?本节:

                    [title] => E ST ELMO RD/SHERATON AVE
                    [link] => http://www.ci.austin.tx.us/fact/default.cfm
                    [description] => AFD - 4700 S Congress Ave - BOX -Structure Fire - Wed, 23 Nov 2016 10:51 PM 
                    [pubdate] => Wed, 23 Nov 2016 10:51 PM 

使用此代码:

echo $xml->channel->item->link;

我正在

Notice:Trying to get property of non-object

2 个答案:

答案 0 :(得分:2)

您使用的是错误代码,请尝试此操作

foreach($xml->channel->item as $key => $product){
   echo $product->link;

或直接

 echo $xml->channel->item->link;

答案 1 :(得分:0)

echo ($xml['channel'])->item->link;  

是解决方案吗?