I have the following :
function Recurse1($xml, $array, $i){
$a = array();
foreach($xml - > children() as $key => $child) {
$array[$key][$i] = $child - > attributes() - > role;
$i++;
$a = array_merge($array, Recurse1($child, $array, $i));
}
return $a;
}
var_dump(Recurse1($xml, array(), 0));
$xml = new SimpleXMLElement(
'<person>
<child role="son1">
<child role="daughter1"/>
</child>
<child role="daughter2">
<child role="son2">
<child role="son3"/>
</child>
</child>
</person>'
);
I get the following:
array(1) {
["child"]=> array(4) {
[0]=> object(SimpleXMLElement)#59 (1) {
[0]=> string(4) "son1"
}
[1]=> object(SimpleXMLElement)#71 (1) {
[0]=> string(9) "daughter2"
}
[2]=> object(SimpleXMLElement)#77 (1) {
[0]=> string(4) "son2"
}
[3]=> object(SimpleXMLElement)#83 (1) {
[0]=> string(4) "son3"
}
}
}
I am trying to get daughter1 but no vail. Any suggestion?I can;t see the error. Thanks in advance!
daughter1, you're just overwriting it.