php - simpleXML how to access a specific element with the same name as others?
<contact:addr>
<contact:street></contact:street>
<contact:street></contact:street>
<contact:street></contact:street>
<contact:city></contact:city>
<contact:pc></contact:pc>
<contact:cc></contact:cc>
</contact:addr>
On the example above we can see that we do have three times the element street; Is there a way, by using simpleX开发者_如何学GoML, to properly access, for example, the second street element?
Thanks in advance, MEM
The element reference in SimpleXML can be accessed as an array (since it is an iterator), meaning that $root->element[1]
will return the second element with name "element" under the root. (and [0]
will return the first, as shown in the SimpleXML examples in the PHP manual.)
You can iterate over all the elements using foreach($root->element as ..)
精彩评论