2017-03-13 56 views
0

我有一個看起來像這樣的XML ...遍歷XML對象的字段值PHP

<fields> 
    <field> 
    <id>86</id> 
    <source>system</source> 
    <type>integer</type> 
    <name>bounce_count</name> 
    <label>Bounce count</label> 
    <validation/> 
    <store-locally>true</store-locally> 
    <display-in-profile>false</display-in-profile> 
    <include-in-export>false</include-in-export> 
    <lma_display>false</lma_display> 
    <newsletters></newsletters> 
    </field> 
</fields> 

我通過每一個標籤的內部<field>值需要循環英寸我試過場標籤內的以下(結果是XML),首先我得到的一切......

$value = $result->field 
$value = simplexml_load_string($value); 

如果我var_dump()這個它看起來如此...

對象(的SimpleXMLElement) #47(11){[「id」] => string(3)「153」[「source」] => string(6)「client」[「type」] => string(6)「string」[「 name「] => string(4)」name「[」label「] => string(4)」Name「[」validation「] => object(SimpleXMLElement)#48(0){} [」store-locally「 ] => string(4)「true」[「display-in-profile」] => string(4)「true」[「include-in-export」] => string(4)「true」[「lma_display」 ] => string(4)「true」[「newsletters」] => object(SimpleXMLElement)#46(0){}}

接下來,我試圖通過循環領域內field

foreach ($value as $single_field) { 
    echo $single_field; 
} 

但我沒有得到任何輸出。如果我var_dump()$single_field我得到了和上面相同的結果。我需要遍歷每個字段中的值。所以對於上面的XML我需要存儲以下值...

86 
system 
integer 
bounce_count 
Bounce Count 
true 
false 
false 
false 

我該怎麼做?

回答

1

如果你想遍歷元素的孩子,你需要在你foreach循環使用SimpleXMLElement::children

foreach ($value->children() as $single_field) { 
    echo $single_field . "\n"; 
}