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

时间:2022-04-15 21:39:01

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.

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

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:

这是一个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
        )

)

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.

我需要构造数据,以便每个父级都有一个'Children'键,这是一个重复的数组数组,不限制父/子/孙子可以拥有的子数量,tree_depth键由自动计算出来。 DBMS,所以我只需要改变数组的结构。

Any pointers greatly appreciated, I have played with usort() and array_walk_recursive to no avail.

任何指针都非常感激,我玩usort()和array_walk_recursive无济于事。

Thanks in advance

提前致谢

1 个解决方案

#1


I think a simple foreach can do the trick here (with the help of references):

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

Set up a $menu associative array $cat_id => $element_details_anb_children:

设置$ 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'] ];
    }
}

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

这应该构建两个数组:你想要的多维数组($ menu)和一个只保存每个类别的引用的平面数组。在每次迭代中,如果类别已经存在,它将类别嵌入其父类中(这就是我保留引用表的原因)。当然,只有在您的初始$ tree数组被排序时(即父项在其子项之前),它才有效。

#1


I think a simple foreach can do the trick here (with the help of references):

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

Set up a $menu associative array $cat_id => $element_details_anb_children:

设置$ 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'] ];
    }
}

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

这应该构建两个数组:你想要的多维数组($ menu)和一个只保存每个类别的引用的平面数组。在每次迭代中,如果类别已经存在,它将类别嵌入其父类中(这就是我保留引用表的原因)。当然,只有在您的初始$ tree数组被排序时(即父项在其子项之前),它才有效。