2017-09-01 117 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->產品 - >兒童()