我有一个simplexmlement,我使用curl在XML文件中读取了它。
尝试从中获取值时,我似乎需要使用嵌套foreach,但在类似的情况下,我可以一次性完成此操作。现在我真的很好奇为什么这不起作用:
它是美味的RSS饲料;
SimpleXMLElement Object
(
[title] => Delicious/chrisvdberge
[link] => http://www.delicious.com/chrisvdberge
[description] => bookmarks posted by chrisvdberge
[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => Diagrammr
[pubDate] => Thu, 06 May 2010 12:40:07 +0000
[guid] => http://www.delicious.com/url/2fc2ed0e870ec9d001b645dfaed0e771#chrisvdberge
[link] => http://www.diagrammr.com/
[comments] => http://www.delicious.com/url/2fc2ed0e870ec9d001b645dfaed0e771
[source] => chrisvdberge's bookmarks
[category] => Array
(
[0] => tagthis
[1] => visualization
[2] => diagrams
)
)
[1] => SimpleXMLElement Object
(
[title] => Ignite Toronto 2: Ryan Coleman - Designing for visual efficiency on Vimeo
[pubDate] => Mon, 18 Jan 2010 08:05:06 +0000
[guid] => http://www.delicious.com/url/9a0de307f3f37baaa25ad20b4dbc4537#chrisvdberge
[link] => http://vimeo.com/8317770
[comments] => http://www.delicious.com/url/9a0de307f3f37baaa25ad20b4dbc4537
[source] => chrisvdberge's bookmarks
[category] => Array
(
[0] => tagthis
[1] => vizthink
)
)
等。
我可以使用嵌套foreach访问所有链接:
foreach ($delicious_res as $item) {
foreach ($item->item as $entry){
echo $entry->link."<br />";
}
}
foreach ($delicious_res->item as $item) {
echo $entry->link."<br />";
}
为了更好地理解SimpleXML,我真的很想知道为什么;)