- var_dump: documentation ( source)
- array_merge: documentation ( source)
- is_iterable: documentation ( source)
<?php
function Recurse1($xml)
{
foreach ($xml->children() as $child)
{
if (is_iterable($child)) {
if (!isset($array)) {
$array = Recurse1($child);
} else {
$array = array_merge($array, Recurse1($child));
}
} else {
$array[] = (string)$xml['role'];
}
}
return $array;
}
$xml = new SimpleXMLElement(
'<person>
<child role="son1">
<child role="daughter1"/>
</child>
<child role="daughter2">
<child role="son2">
<child role="son3"/>
</child>
</child>
</person>');
var_dump(Recurse1($xml));