多维数组从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) –