多維數組從XML文件Iter項目通過PHP
<items>
<item type="blue" />
<item type="red" />
<item type="blue" />
<item type="red" />
<item type="blue" />
<item type="red" />
<item type="blue" />
<item type="red" />
<item type="blue" />
<item type="red" />
<item type="blue" />
<item type="red" />
</items>
我通過了SimpleXML得到下面的數組:
object(SimpleXMLElement)#1 (1) {
["item"]=> array(12) {
[0]=> object(SimpleXMLElement)#2 (1) {
["@attributes"]=> array(1) {
["type"]=> string(4) "blue"
}
}
[1]=> object(SimpleXMLElement)#3 (1) {
["@attributes"]=> array(1) {
["type"]=> string(3) "red"
}
}
[2]=> object(SimpleXMLElement)#4 (1) { … }
[3]=> object(SimpleXMLElement)#5 (1) { … }
[4]=> object(SimpleXMLElement)#6 (1) { … }
[5]=> object(SimpleXMLElement)#7 (1) { … }
[6]=> object(SimpleXMLElement)#8 (1) { … }
[7]=> object(SimpleXMLElement)#9 (1) { … }
[8]=> object(SimpleXMLElement)#10 (1) { … }
[9]=> object(SimpleXMLElement)#11 (1) { … }
[10]=> object(SimpleXMLElement)#12 (1) { … }
[10]=> object(SimpleXMLElement)#12 (1) { … }
[11]=> object(SimpleXMLElement)#13 (1) { … }
} }
我想通過全項環和選擇的前三個與藍色的類型:
1) Iter through all items
2) Check whether type == 'blue'
3) If yes, append the whole item-structure to another array or echo something
我這樣做了很多與Python,哪裏可以在所解析的對象導航(一個字典陣列)以k現在只不過是XML的基本結構。但在PHP中,我不明白陣列中的元素是如何處理的。
當我在這裏看教程或其他問題時,在我看來,沒有像xpath這樣的路徑系統。我還沒有找到像我這樣的basix例子。
這是一個重複:SimpleXML的:選擇具有一定屬性值的元素(https://stackoverflow.com/questions/992450/simplexml-selecting-elements-which-have-a-certain-attribute-value) –