简体   繁体   English

php foreach的XML提取未循环

[英]XML extract with php foreach is not looping

I'm trying to make this loop, but it is only returning the first values. 我正在尝试进行此循环,但它仅返回第一个值。

XML XML

<catalog>
<dd>
    <categoryassignment categoryid="3d-magic" productid="000000000000109330_PURPLE/ BLUE"/>

    <categoryassignment categoryid="3d-magic" productid="000000000000109330_PURPLE/ BLUE/ ORANGE"/>

    <categoryassignment categoryid="3d-magic" productid="000000000000109330_RED/ GREEN/ YELLOW"/>

    <categoryassignment categoryid="3d-magic" productid="000000000000109335_BLUE/YELLOW"/>

    <categoryassignment categoryid="3d-magic" productid="000000000000109335_PURPLE/ORANGE"/>

    <categoryassignment categoryid="3d-magic" productid="000000000000115881_GREEN/PURPLE"/>

    <categoryassignment categoryid="3d-magic" productid="000000000000115881_ORANGE/BLUE"/>

    <categoryassignment categoryid="3d-magic" productid="000000000000115881_RED/YELLOW"/>

    <categoryassignment categoryid="3d-magic" productid="000000000109329001"/>

    <categoryassignment categoryid="3d-magic" productid="000000000109329002"/>

    <categoryassignment categoryid="3d-magic" productid="000000000109335001"/>

    <categoryassignment categoryid="3d-magic" productid="000000000111568001"/>

    <categoryassignment categoryid="3d-magic" productid="000000000111568002"/>
</dd>

PHP PHP

$xml=simplexml_load_file("store.xml") or die("Failed to create an object");

foreach($xml->children() as $books) { 
echo $books->categoryassignment['categoryid'];echo $books->categoryassignment['productid'];echo "<br>";}

From the example here: https://www.w3schools.com/php/php_xml_simplexml_get.asp 从此处的示例中: https : //www.w3schools.com/php/php_xml_simplexml_get.asp

You'll need to change how you are referencing the data. 您需要更改引用数据的方式。 If you first do a quick debug by doing: 如果您首先通过以下方法进行快速调试:

print_r($xml);

You will see that you have an object like this: 您将看到您有一个像这样的对象:

SimpleXMLElement Object
(
    [dd] => SimpleXMLElement Object
        (
            [categoryassignment] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [categoryid] => 3d-magic
                                    [productid] => 000000000000109330_PURPLE/ BLUE
                                )
                        )
                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [categoryid] => 3d-magic
                                    [productid] => 000000000000109330_PURPLE/ BLUE/ ORANGE
                                )
                        )
         ... etc ...

With this knowledge, you can then easily see where you need to put your foreach . 有了这些知识,您就可以轻松地了解将foreach放在哪里。 It would be like this (with a slight change to $books inside): 就像这样(里面的$books稍有变化):

foreach($xml->dd->categoryassignment as $books) { 
    echo $books['categoryid'];
    echo ' ';
    echo $books['productid'];
    echo "<br>\n";
}

And that results as: 结果是:

 3d-magic 000000000000109330_PURPLE/ BLUE 3d-magic 000000000000109330_PURPLE/ BLUE/ ORANGE 3d-magic 000000000000109330_RED/ GREEN/ YELLOW 3d-magic 000000000000109335_BLUE/YELLOW 3d-magic 000000000000109335_PURPLE/ORANGE 3d-magic 000000000000115881_GREEN/PURPLE 3d-magic 000000000000115881_ORANGE/BLUE 3d-magic 000000000000115881_RED/YELLOW 3d-magic 000000000109329001 3d-magic 000000000109329002 3d-magic 000000000109335001 3d-magic 000000000111568001 3d-magic 000000000111568002 

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

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