【发布时间】:2019-03-31 22:24:29
【问题描述】:
我有以下:
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>'
);
我得到以下信息:
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"
}
}
}
我想得到女儿1,但没有成功。有什么建议吗?我看不到错误。 提前致谢!
【问题讨论】:
-
那么你想要的输出到底是什么?你想维护元素的层次结构还是想要一个扁平化或半扁平化的数组?我想发布一个答案,但你的问题有点不清楚。
-
你正在得到
daughter1,你只是在覆盖它。