Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在数组中查找某些XML项?_Php_Xml_Arrays_Parsing - Fatal编程技术网

Php 如何在数组中查找某些XML项?

Php 如何在数组中查找某些XML项?,php,xml,arrays,parsing,Php,Xml,Arrays,Parsing,如何从XML文档返回标记为guid的所有项目?例如: [guid] => http://www.motorauthority.com/blog/1060293_aston-martin-v12-zagato-2011-concorso-deleganza-villa-deste 这是我的密码: $source = 'http://feeds.feedburner.com/motorauthority2?format=xml'; $dom = new DOMDocument(); @$do

如何从XML文档返回标记为
guid
的所有项目?例如:

[guid] => http://www.motorauthority.com/blog/1060293_aston-martin-v12-zagato-2011-concorso-deleganza-villa-deste
这是我的密码:

$source = 'http://feeds.feedburner.com/motorauthority2?format=xml';
$dom = new DOMDocument();
@$dom->loadHTMLFile($source);
$xml = simplexml_import_dom($dom);
$blog = $xml->xpath("//channel");
print_r($blog);
以下是打印的输出:

Array
(
    [0] => SimpleXMLElement Object
        (
            [language] => en
            [title] => High Gear Media Network Feed
            [description] => Latest news, reviews, and more from around the High Gear Media network of sites
            [image] => SimpleXMLElement Object
                (
                    [link] => SimpleXMLElement Object
                        (
                        )

                    [url] => http://www.motorauthority.com/images/logo-footer.jpg
                    [title] => MotorAuthority
                )

            [lastbuilddate] => Fri, 20 May 2011 04:55:17 -0400
            [generator] => High Gear Media
            [link] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [atom10] => http://www.w3.org/2005/Atom
                                    [rel] => self
                                    [type] => application/rss+xml
                                    [href] => http://feeds.feedburner.com/MotorAuthority2
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [atom10] => http://www.w3.org/2005/Atom
                                    [rel] => hub
                                    [href] => http://pubsubhubbub.appspot.com/
                                )

                        )

                )

            [info] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [uri] => motorauthority2
                        )

                )

            [explicit] => no
            [subtitle] => Latest news, reviews, and more from around the High Gear Media network of sites
            [meta] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [xhtml] => http://www.w3.org/1999/xhtml
                            [name] => robots
                            [content] => noindex
                        )

                )

            [feedflare] => Array
                (
                    [0] => Subscribe with My Yahoo!
                    [1] => Subscribe with NewsGator
                    [2] => Subscribe with My AOL
                    [3] => Subscribe with Bloglines
                    [4] => Subscribe with Netvibes
                    [5] => Subscribe with Google
                    [6] => Subscribe with Pageflakes
                    [7] => Subscribe with Plusmo
                    [8] => Subscribe with The Free Dictionary
                    [9] => Subscribe with Bitty Browser
                    [10] => Subscribe with NewsAlloy
                    [11] => Subscribe with Live.com
                    [12] => Subscribe with Excite MIX
                    [13] => Subscribe with Attensa for Outlook
                    [14] => Subscribe with Webwag
                    [15] => Subscribe with Podcast Ready
                    [16] => Subscribe with Flurry
                    [17] => Subscribe with Wikio
                    [18] => Subscribe with Daily Rotation
                )

            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => Aston Martin V12 Zagato: 2011 Concorso d'Eleganza Villa d'Este
                            [description] => The weekend is finally upon us and that means the 2011 Concorso d’Eleganza Villa d’Este is kicking off, along with all that comes with it. Yesterday we saw a sneak peek at the new Ferrari Superamerica 45, built under the automaker’s Special Projects division and destined for a home in New York. Today we have another coachbuilt...]]>
                            [pubdate] => Fri, 20 May 2011 04:55:17 -0400
                            [link] => SimpleXMLElement Object
                                (
                                )

                            [guid] => http://www.motorauthority.com/blog/1060293_aston-martin-v12-zagato-2011-concorso-deleganza-villa-deste

您需要的XPath表达式是

/rss/channel/item/guid

请注意,
simplexml\u load\u file()
可以处理URI。没有必要通过
DOMDocument

您已经将XML转换为PHP数组,因此它应该只是递归地扫描数组以查找所需的密钥

像这样的东西应该可以做到:

<?php
function array_key_search_recursive($array,$keytofind) {
    $output = array();
    foreach($array as $key=>$value) {
        if($key == $keytofind) {
            $output[] = $value;
        } elseif(is_array($value)) {
            $output = array_merge($output, array_key_search_recursive($value,$keytofind));
        }
    }
    return $output;
}

?>

(请注意,我还没有测试该代码;我只是将其组装在一起,所以它可能有bug)

[编辑]发布此答案后,我注意到的第一件事是您实际上尚未将其转换为数组;它仍然是一个对象(只是将其格式化为数组)。上面的内容应该仍然有效,但您需要将其作为一个对象来使用;可能最简单的方法就是在
foreach
循环中将
$array
$value
转换为类型数组