Stack Exchange
Best way to build multidimensional array (categories ; sub-categories; sub-sub-categories etc.)
Hello I have this response from my sql query :  
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Category #1
            [parent] => 0
        )

    [1] => Array
        (
            [id] => 2
            [name] => Category #2
            [parent] => 0
        )

    [2] => Array
        (
            [id] => 3
            [name] => Category #3
            [parent] => 0
        )

    [3] => Array
        (
            [id] => 4
            [name] => Category #4
            [parent] => 0
        )

    [4] => Array
        (
            [id] => 5
            [name] => Sub category of category #1
            [parent] => 1
        )

    [5] => Array
        (
            [id] => 6
            [name] => Sub category of category #2
            [parent] => 2
        )

    [6] => Array
        (
            [id] => 7
            [name] => Sub category of category #6 (which is sub category of category #2)
            [parent] => 6
        )
)

After converting the response must looks like this :  
array
(
    array
        (
            [id] => 1
            [name] => Category #1
            [parent] => 0
            [children]=> array
                (
                    [id] => 5
                    [name] => Sub category of category #1
                    [parent] => 1
                )
        )

    array
        (
            [id] => 2
            [name] => За дома
            [parent] => 0
            [children]=> array
                (
                    [id] => 6
                    [name] => Sub category of category #2
                    [parent] => 2
                    [children] => array
                        (
                            [id] => 7
                            [name] => Sub category of category #6 (which is sub category of category #2)
                            [parent] => 6
                        )
                )
        )

    array
        (
            [id] => 3
            [name] => Category #3
            [parent] => 0
        )

    array
        (
            [id] => 4
            [name] => Category #4
            [parent] => 0
        )
)

My question is - what is the best way to build such a multidimensional array (in php for example).

with a recursion. Since, we do not know your input data, it's hard to tell.

Try something like this
$arr = 'Your array';

$new = array();
foreach ($arr as $a){
    $new[$a['parent']][] = $a;
}
$data = createMyTree($new, array($arr[0]));
print_r($data);

function createMyTree(&$list, $parent){
    $tree = array();
    foreach ($parent as $k=>$l){
        if(isset($list[$l['id']])){
            $l['children'] = createMyTree($list, $list[$l['id']]);
        }
        $tree[] = $l;
    } 
    return $tree;
}


You can just store the data in a regular array and use code to traverse it back. So each node has its parentId in it, and as long as the parentId is not 0 you traverse back another parent till you get to the root parent of a given node.
you really do not need to think of it as a multi-dimensional array; which you will not be able to do any how because of the infinite dimensions.  In other languages, such as C, you could just build a tree implementation (a classic data structure) using pointers. But in PHP i would just use a flat array, store the parentId, and use code to recurse.
