2014-02-16 54 views
0

我有以下腳本讀取XML。但它在for循環之後停止。如果我刪除循環它運行完美。簡單的xml不支持循環到foreach

我做錯了什麼,或者我能以其他方式做到這一點?

腳本

$xml=simplexml_load_file("test.xml"); 

foreach($xml->product as $child){ 
    echo 'Title:'.$child->name.'<br>';//title 
    echo 'Price:'.$child->price.'<br>';//title 
    echo 'image:'.$child->images->image.'<br>';//image 
    echo 'category:'.$child->categories->category.'<br>';//categorie 

    foreach($child->properties->property as $property){    
     if($child->properties->property->attributes()->name =='brand'){ 
      echo $child->properties->property->value.'<br>'; 
     } 
    } 
} 

XML

<product ID="000000000000052993"> 
    <name> Title product</name> 
    <price currency="EUR">29.99</price> 
    <URL>http://www.url.nl</URL> 
    <images> 
     <image>product/5/2/52993_2.jpg</image> 
    </images> 
    <description><![CDATA[description.]]> 
    </description> 
    <categories> 
     <category>catt</category> 
    </categories> 
    <properties> 
     <property name="brand"> 
      <value>PHILIPS</value> 
     </property> 
     <property name="deliveryTime"> 
      <value>5</value> 
     </property> 
     <property name="User1"> 
     </property> 
     <property name="User2"> 
     </property> 
     <property name="shipping_cost"> 
      <value>0.00</value> 
     </property> 
     <property name="User3"> 
     </property> 
     <property name="EAN"> 
      <value>8727900831733</value> 
     </property> 
    </properties> 
    <variations/> 
</product> 
+0

把它放在一個數組應該工作 –

回答

0

讓它另一個foreach循環:

foreach($child->properties as $property){ 
    if($property->attributes()->name == 'brand'){ 
     echo $property->value; 
    } 
    if($property->attributes()->name == 'deliveryTime'){ 
     echo $property->value; 
    } 
} 
+0

謝謝,但我想過濾$ child-> prope rties-> property [$ x] - > attributes() - >名稱,這也可能是一個foreach? – Bas

+0

是的。你想達到什麼目標,我會向你提供代碼。 – Banago

+0

我想篩選品牌和交付時間,如for循環 – Bas