2017-09-01 76 views
0

我有这样的表结构,我需要摆动她的foreach循环来提取所有848元素。问题是循环只显示一个项目,而不是坐在那里的实际数量。随着定期和定期替代作品的方式,虽然写同样的东西在我看来是更长和更丑。simplexml_load_file - foreach显示一个时间循环

阵列结构:

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']; 

     } 

但这不起作用,环形折返一个记录。

+2

这将是更容易,如果你解决包含您的XML文件的副本(或部分)而不是对象结构。 –

回答

0
$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->产品 - >儿童()