3v4l.org

run code in 300+ PHP versions simultaneously
<?php function Recurse1($xml) { $array = [(string)$xml['role']]; foreach ($xml->children() as $child) { $array = array_merge($array, Recurse1($child)); } 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));

preferences:
34.7 ms | 402 KiB | 5 Q