我一直在尝试修改的预排序树遍历模式,我的测试用例代码按预期返回结果,但是我无法将二维数组转换为多维数组来呈现它。
这是一个 3 级菜单结果的示例,我需要将其转换为多维数组,以便可以在 TAL 中对其进行迭代:
Array
(
[0] => Array
(
[CategoryID] => 1
[ParentID] => 0
[CategoryName] => Default Parent
[lt] => 1
[rt] => 14
[tree_depth] => 1
)
[1] => Array
(
[CategoryID] => 8
[ParentID] => 1
[CategoryName] => SysAdmin
[lt] => 2
[rt] => 7
[tree_depth] => 2
)
[2] => Array
(
[CategoryID] => 2
[ParentID] => 8
[CategoryName] => Linux
[lt] => 3
[rt] => 4
[tree_depth] => 3
)
[3] => Array
(
[CategoryID] => 3
[ParentID] => 8
[CategoryName] => Windows
[lt] => 5
[rt] => 6
[tree_depth] => 3
)
[4] => Array
(
[CategoryID] => 5
[ParentID] => 1
[CategoryName] => Code
[lt] => 8
[rt] => 13
[tree_depth] => 2
)
[5] => Array
(
[CategoryID] => 6
[ParentID] => 5
[CategoryName] => PHP
[lt] => 9
[rt] => 10
[tree_depth] => 3
)
[6] => Array
(
[CategoryID] => 7
[ParentID] => 5
[CategoryName] => Perl
[lt] => 11
[rt] => 12
[tree_depth] => 3
)
)
我需要对数据进行结构化,以便每个父母都有一个“孩子”键,它是一个重复的数组数组,对父母/孩子/孙子可以拥有的孩子数量没有限制,tree_depth 键由DBMS,所以我只需要改变数组的结构。
非常感谢任何指针,我玩过 usort() 和 array_walk_recursive 无济于事。
提前致谢