According to the manual:
Returns a SimpleXMLElement element, whether the node has children or not.
But first of all, this doesn’t make sense to me. children() should surely return an array of SimpleXMLElements. If the node has more than one child, how can returning a single SimpleXMLElement possibly represent that?
Second of all, that doesn’t tie in with the example that follows in the manual:
foreach ($xml->children() as $second_gen)
echo ' The person begot a ' . $second_gen['role'];
How can you foreach through a SimpleXMLElement? I thought you could only do that with arrays? Also, what the heck’s going on with the $second_gen values? So children() returns an array of associative arrays…?
There must be some somewhat deep PHP grammar rules going on here that I’m not aware of so please explain or point me to the relevant manual pages.
Why not try for yourself (demo):
Then do
Output
Then do
Output
Well, that’s wrong. You can foreach any Traversable (e.g. objects and arrays, etc) and any object implementing the Iterator interface. Here is the class signature of
SimpleXmlElementNo, it doesnt. It returns a
SimpleXmlElement. Accessing aSimpleXmlElementby Array Access fetches attributes from the current node (or accesses a child node by position if numeric), e.g.This is somewhat non-obvious because the
SimpleXmlElementdoesnt implementArrayAccessbut it’s how it is implemented in C. See http://www.php.net/manual/en/simplexml.examples-basic.php for more examples.