将 MPTT 结果集排序为多维数组 PHP

发布于 2024-07-25 14:27:21 字数 1961 浏览 3 评论 0原文

我一直在尝试修改后的前序树遍历 模式,我的测试用例代码正在按预期返回结果,但是我是 无法将二维数组转换为多维数组来呈现它。

这是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2024-08-01 14:27:21

我认为一个简单的 foreach 可以在这里做到这一点(在引用的帮助下):

设置一个 $menu 关联数组 $cat_id => $element_details_anb_children

$menu = array(); $ref = array();
foreach( $tree as $d ) {
    $d['children'] = array();
    if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
        $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ] = $d;
        $ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ];
    } else { // we don't have a reference on its parent => put it a root level
        $menu[ $d['CategoryID'] ] = $d;
        $ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
    }
}

这应该构建两个数组:您想要的多维数组($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:

$menu = array(); $ref = array();
foreach( $tree as $d ) {
    $d['children'] = array();
    if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
        $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ] = $d;
        $ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ];
    } else { // we don't have a reference on its parent => put it a root level
        $menu[ $d['CategoryID'] ] = $d;
        $ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
    }
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文