循环 SimpleXMLElement 对象只需 2 步
我有一个 SimpleXMLElement,我使用curl 在 XML 文件中读取它。 尝试从中获取值时,我似乎需要使用嵌套的 foreach,但在类似的情况下,我能够一次性完成此操作。现在我真的很好奇为什么这不起作用:
它是美味的 RSS feed;
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 ;)
I've got a SimpleXMLElement for which I read in a XML file using curl.
Trying to get the values out of it I seem to need to use a nested foreach, but in a similar situation I was able to do this in one go. Now I'm really curious why this isn't working:
Its the delicious RSS feed;
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
)
)
etc.
I can access all the links by using a nested foreach:
foreach ($delicious_res as $item) {
foreach ($item->item as $entry){
echo $entry->link."<br />";
}
}
But I don't understand why I cant use this (which I can in another situation)
foreach ($delicious_res->item as $item) {
echo $entry->link."<br />";
}
I really like to understand why in order to get a better understanding of SimpleXML in general ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用:
因为项目集合位于
和
标记内(
标签可以省略,因为它是文档的根节点)。另请注意:如果您没有做任何花哨的事情,您可能不需要使用
curl
来获取 rss feeds xml 内容。只需使用simplexml_load_file
并将 url 作为参数:You have to use:
Because the collection of items is inside the
<rss>
and<channel>
tag (the<rss>
tag can be omitted as it is the root node of the document).On another note: if you are not doing anything fancy, you probably don´t need to use
curl
to fetch the rss feeds xml content. Just usesimplexml_load_file
with the url as an argument: