将 MPTT 结果集排序为多维数组 PHP
我一直在尝试修改后的前序树遍历 模式,我的测试用例代码正在按预期返回结果,但是我是 无法将二维数组转换为多维数组来呈现它。
这是一个 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
)
)
我需要构造数据,以便每个父级都有一个“Children”键,它是重复的数组数组,对父/子/孙子可以拥有的子代数量没有限制,tree_depth 键由 DBMS 自动计算出来,所以我只需要更改数组的结构即可。
非常感谢任何指针,我已经使用了 usort() 和 array_walk_recursive 但无济于事。
提前致谢
I have been experimenting with the Modified Pre-Order Tree Traversal
Pattern, my test case code is returning the results as expected however I am
having trouble converting the 2D array into a multi-dimensional array to present it.
Here is an example of a 3 level menu result, I need to convert this into a multi-dimensional array so that I can iterate it in 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
)
)
I need to structure the data so that every parent has a 'Children' key which is an array of arrays repeated, with no limitation on the amount of children a parent/child/grandchild can have, the tree_depth key is worked out automatically by the DBMS, so I simply need to alter the structure of the array.
Any pointers greatly appreciated, I have played with usort() and array_walk_recursive to no avail.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为一个简单的
foreach
可以在这里做到这一点(在引用的帮助下):设置一个
$menu
关联数组$cat_id => $element_details_anb_children
:这应该构建两个数组:您想要的多维数组(
$menu
)和一个仅保存每个类别的引用的平面数组。 在每次迭代中,如果类别已经存在,它会将类别嵌套到其父类别中(这就是我保留引用表的原因)。 当然,只有当您的初始$tree
数组是有序的(即父级位于其子级之前)时,它才有效。I think a simple
foreach
can do the trick here (with the help of references):Set up a
$menu
associative array$cat_id => $element_details_anb_children
:This should build two arrays: the multidimensional array you want (
$menu
) and a flat array which only holds references for each category. On each iteration it nests the category into its parent if it already exists (which is why I keep the reference table). Of course it works only if your initial$tree
array is ordered (i.e. the parent comes before its children).