像树一样递归地重构具有id和parent_id的数组

时间:2013-11-27 09:50:56

标签: php arrays

我有一个类似这种模式的数组(类别和子类别):array('parent_id1'=>array('id1','id2','id3'))

这是我的确切数组:

       $categories =  Array
(
    [0] => Array
        (
            [0] => 12
            [1] => 13
            [2] => 14
            [3] => 15
            [4] => 16
            [5] => 17
            [6] => 18
            [7] => 19
            [8] => 20
        )

    [12] => Array
        (
            [0] => 21
            [1] => 22
            [2] => 23
            [3] => 24
            [4] => 25
            [5] => 26
            [6] => 27
        )

    [14] => Array
        (
            [0] => 36
        )

    [26] => Array
        (
            [0] => 28
            [1] => 29
            [2] => 30
            [3] => 31
            [4] => 32
        )

    [28] => Array
        (
            [0] => 33
            [1] => 34
        )

    [33] => Array
        (
            [0] => 35
        )

    [36] => Array
        (
            [0] => 37
        )

)
  • parent_id = 0的类别是所有类别的父亲。

我想像这样的树重构这个数组:

    Array
    (
    [12] => Array
        (
            [21] => Array
                (
                )

            [22] => Array
                (
                )

            [23] => Array
                (
                )

            [24] => Array
                (
                )

            [25] => Array
                (
                )

            [26] => Array
                (
                    [28] => Array
                        (
                            [33] => Array
                                (
                                    [35] => Array
                                        (
                                        )

                                )

                            [34] => Array
                                (
                                )

                        )

                    [29] => Array
                        (
                        )

                    [30] => Array
                        (
                        )

                    [31] => Array
                        (
                        )

                    [32] => Array
                        (
                        )

                )

            [27] => Array
                (
                )

        )

    [13] => Array
        (
        )

    [14] => Array
        (
            [36] => Array
                (
                    [37] => Array
                        (
                        )

                )

        )

    [15] => Array
        (
        )

    [16] => Array
        (
        )

    [17] => Array
        (
        )

    [18] => Array
        (
        )

    [19] => Array
        (
        )

    [20] => Array
        (
        )

)

我怎么能递归地做到这一点?

1 个答案:

答案 0 :(得分:1)

<?php
function mysort($arr, $key = 0){
    $ids = $arr[$key];
    $ret = array();
    sort($ids);
    foreach($ids as $id){
        $ret[$id] = empty($arr[$id]) ? array() : mysort($arr, $id);
    } 
    return $ret;
}

print_r(mysort($categories));