aus einer XML-DateiIter durch mehrdimensionales Array in 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>
erhalte ich die folgende Reihe von 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) { … }
} }
Ich möchte alle Positionen durchlaufen und wählen Sie die ersten drei mit der Typ blau:
1) Iter through all items
2) Check whether type == 'blue'
3) If yes, append the whole item-structure to another array or echo something
ich diese Menge mit Python, wo ich in dem geparsten Objektes (a dict Array) von k navigieren nichts mehr als die Grundstruktur des XML. Aber in PHP verstehe ich nicht, wie die Elemente im Array angesprochen werden.
Wenn ich hier Tutorials oder andere Fragen anschaue, scheint mir, dass es kein Pfadsystem wie xpath gibt. Ich habe kein Basix-Beispiel wie meines gefunden.
Dies ist ein Duplikat: [SimpleXML: Auswählen von Elementen, die ein bestimmtes Attribut Wert] (https://stackoverflow.com/questions/992450/simplexml-selecting-elements-which-haben-einen-bestimmten-attribute-value) –