-1

I have an array like this pattern ( categories and sub categories ): array('parent_id1'=>array('id1','id2','id3'))

this is my exact array :

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

)
  • categories with parent_id=0 are the father of all categories.

I want to restructure this array like a tree like that:

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

)

how can I recursively do that ?

Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
  • No, you don't want to "sort" it, you want to *restructure* it. – deceze Nov 27 '13 at 09:53
  • possible duplicate of [Recursive function to generate multidimensional array from database result](http://stackoverflow.com/questions/8587341/recursive-function-to-generate-multidimensional-array-from-database-result) – deceze Nov 27 '13 at 09:53

1 Answers1

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));
jhdxr
  • 166
  • 1
  • 4