simplexml_load_file- foreach显示一个时间循环



我有一个这样的表结构,我需要将她摆动foreach循环以提取所有848个元素。问题是循环仅显示一个项目,而不是坐在那里的实际数量。虽然我认为同样的东西更长且丑陋。

数组结构:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [language] => pol
            [currency] => PLN
        )
    [product] => Array
        (
            [0] => SimpleXMLElement Object
                [@attributes] => Array
                        (
                            [id] => 8
                        )
            [1] => SimpleXMLElement Object
                [@attributes] => Array
                        (
                            [id] => 4
                        )       
            [etc...]
        )

我尝试这种方式

$chairXML = simplexml_load_file('toparser.xml');
        foreach ($chairXML->children() as $children) {
            echo $children->product['id'];
        }

但这不起作用,循环返回一个记录。

$xml = ' 
<xml>
 <product>
   <cherry>1</cherry>
   <apple>3</apple>
   <orange>4</orange>
 </product>
</xml>';
$simpleXmlObj = simplexml_load_string($xml);
foreach ($simpleXmlObj->product->children() as $children) {
    print_r($children);
}

结果

SimpleXMLElement Object
(
    [0] => 1
)
SimpleXMLElement Object
(
    [0] => 3
)
SimpleXMLElement Object
(
    [0] => 4
)

我认为您在$ simplexmlobj->儿童中的问题()尝试$ simplexmlobj-> product->儿童()

最新更新