2012-04-07 26 views
1

我正在开发一个使用PHP的应用程序。一些示例代码在这里。我如何访问嵌套数组项php

$url = "http://localhost:8080/?Xml"; 

$xml = simplexml_load_file($url); 

foreach($xml->result->doc as $doc){ 
echo "<pre>".print_r($doc)."</pre>"; 
} 

其中的一个项目看起来像我所要求的。

SimpleXMLElement Object ([@attributes] => Array ([score] => 4.112774 [pos] => 0) [field] => Array ([0] => http://www.w3schools.com/htmldom/dom_examples.asp [1] => text/html [2] => Free HTML XHTML CSS JavaScript jQuery XML DOM XSL XSLT RSS AJAX ASP .NET PHP SQL tutorials, references, examples for web building. [3] => html,css,tutorial,html5,dhtml,css3,xsl,xslt,xhtml,javascript [4] => www.w3schools.com [5] => en) [snippet] => Array ([0] => DOM Examples [1] => DOM HOME DOM Intro DOM Nodes DOM Node Tree DOM Methods DOM Node Access DOM Node Info DOM How To DOM Events DOM Summary DOM...)) 

我想知道我如何访问数组项目?

+0

这跟这里是一样的问题吗? http://stackoverflow.com/questions/2867575/get-value-from-simplexmlelement-object – Config 2012-04-07 07:58:23

回答

1
foreach($doc->attributes() as $key => $value) { 
    echo "$key: $value\n"; 
}