2
<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 simpleXML, to properly access, for example, the second street element?

Thanks in advance, MEM

1 Answer 1

5

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 ..)

Sign up to request clarification or add additional context in comments.

5 Comments

@Gordon: Sir with 32135 reputation... I do believe that THAT is a tricky comment. I will have a look into the above. @fiskfisk thanks for the relation between the fact SimpleXML and iterator. ;) Really appreciated.
@Gordon: I do have something like this: $xmlObj->command->create->children(self::OBJ_URI_CONTACT)->create->postalInfo->addr->street and it works with the namespaces. Will not: $xmlObj->command->create->children(self::OBJ_URI_CONTACT)->create->postalInfo->addr->street[0] work ?
@all - for my previous question - a boolean answer may suffice. :)
@MEM It should work this way, but I found SimpleXml to be quirky when it comes to namespace support, though I hear it has been greatly improved with recent versions of PHP. Just give it a try.
@Gordon - Tested. Worked perfectly. ;) Yeah! :D Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.