我有一组菜单项,如下所示:
[
[ "cat" => [ "subcat" => [ "subsubcat1" => $item]]],
[ "cat" => [ "subcat" => [ "subsubcat2" => $item2]]],
[ "cat" => [ "subcat2" => [ "subsubcat3" => $item3]]],
[ "cat" => [ "subcat2" => [ "subsubcat3" => $item4]]],
[ "cat2" => [ "subcat2" => [ "subsubcat3" => $item5]]],
];
我希望将它们组合起来:
[
"cat" =>
[
"subcat" => [
"subsubcat1" => [ $item ],
"subsubcat2" => [ $item2 ],
],
"subcat2" => [
"subsubcat3" => [ $item3, $item4 ]
]
],
"cat2" => [
"subcat2" => [
"subsubcat3" => [ $item5 ]
]
]
];
所以我可以像这样生成一个xml结构:
<menu>
<category id="cat">
<category id="subcat2">
<category id="subsubcat1">
<menuitem name="{ $item['title'] }" />
</category>
</category>
[...]
</category>
</menu>
使用DOM可能省略了第二步,但我无法解决这个问题。我尝试了array_merge_recursive(... $ items),但这也会传播$ item。
如果最后一个嵌套数组的最后一个值是另一个类型(在本例中是一个类),我如何合并这些多维数组?密钥可以被覆盖(在这种情况下没有重复的subcat2,除非父级不同),但是需要追加最后一个数组($ item,$ item2等)中的值。
答案 0 :(得分:1)
这是一个递归函数,它将添加具有该结构的项目。
ALTER TABLE TABLE2 ADD INDEX (SE_ID, LOAD_DT, ID);
您可以将它应用于外部数组,如下所示:
function addItem($item, &$category) {
if (!is_array($item)) {
$category[] = $item;
} else {
$value = reset($item);
$key = key($item);
addItem($value, $category[$key]);
}
}