From an XML-file
<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>
I get the following array by 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) { … }
} }
I want to loop through all items and choose the first three with the type of blue:
1) Iter through all items
2) Check whether type == 'blue'
3) If yes, append the whole item-structure to another array or echo something
I do this alot with Python, where I can navigate in the parsed object (a dict array) by knowing nothing more than the basic structure of the XML. But in PHP I don't understand how the elements in the array are addressed.
When I look at tutorials or other questions here, it seems to me that there is no path-system like xpath. I haven't found a basix example like mine.