Array and Object
first of all, SimpleXMLElement is not an array, its a class. It does
support iteration via the foreach construct though. the foreach iteration
will only go through the first set of elements in the structure, that is it
will only cover the children of the root element. if you want to traverse
all elements of the structure recursively, use SimpleXMLIterator (and
RecursiveIteratorIterator) from SPL.
heres a simple xample to get you started: <?php
$xml = '<m><a><b/></a><c/></m>'; $m = new SimpleXMLElement($xml);
foreach($m as $k => $v) { echo "k:$k => v:$v" . PHP_EOL; } /* outputs
k:a => v:
k:c => v:
*/
$m = new SimpleXMLIterator($xml); foreach(new RecursiveIteratorIterator($m,
RecursiveIteratorIterator::CHILD_FIRST) as $k => $v) { echo "k:$k => v:$v" . PHP_EOL; } /* outputs
k:b => v:
k:a => v:
k:c => v:
*/
-nathan
|