简体   繁体   English

在PHP中,使用simplexml_load_file&foreach遍历所有xml节点和子节点

[英]in PHP, loop through all xml nodes and subnodes using simplexml_load_file & foreach

Given this XML document 给出此XML文档

My code: 我的代码:

    $xml = simplexml_load_file($url);
    $table = array();
    foreach ($xml->entry as $item) {
        foreach ($item->summary->div->blockquote as $block) {

                $element = array();
                $element['title'] = $block->p;
                $element['updated'] = strtotime($item->updated, time());
                $table[$element['updated']] = $element;

        }
    }

I want to insert all "p" values of the "blockquote" node and put them into an array. 我想插入“ blockquote”节点的所有“ p”值,并将它们放入数组中。 But it only gets the last "blockquote" node, not all. 但是它仅获得最后一个“ blockquote”节点,而不是全部。

I get this: 我得到这个:

Warning: Invalid argument supplied for foreach() on line foreach ($item->summary->div->blockquote as $block) {

I need help. 我需要帮助。

This part of code: 这部分代码:

$element['updated'] = strtotime($item->updated, time());

Seems to be always the same for all the elements and that may be overriding all the array. 对于所有元素似乎总是相同的,并且可能覆盖所有数组。

Your $item seems to be always the same inside your second loop. 您的$item在第二个循环中似乎总是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM