Home > Mobile >  How to make arrays in to arrays but like tree
How to make arrays in to arrays but like tree

Time:11-18

I have

`<?php 
$arr = [['id'=> 1, 'parent_id' => '-', 'name' => 'id1'],
['id' => 2, 'parent_id' => 1, 'name'=> 'id2' ],
['id' => 3, 'parent_id' => 1, 'name'=> 'id3' ],
['id' => 4, 'parent_id' => '-', 'name'=> 'id4' ]
,['id' => 5,'parent_id' => 2, 'name'=> 'id5' ],
['id' => 6, 'parent_id' => 3, 'name'=> 'id6' ],
['id' => 7, 'parent_id' => '-', 'name'=> 'id7' ],
['id' => 8, 'parent_id' => 3, 'name'=> 'id8' ],
['id' => 9, 'parent_id' => 4, 'name'=> 'id9' ], 
['id' => 10, 'parent_id' => 9, 'name'=> 'id10' ]];
// 1-2-5
// |-3-6
//---|-8
//------
// 4-9-10
//---
// 7
$new = array();
foreach ($arr as $a){
    $new[$a['parent_id']][] = $a;
}
$tree = createTree($new, array($arr[0]));
print_r($tree);

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

`

My question is how to write the array list in to array but like a tree I managed to do the first family.

so my problem is this recursive action doesn't repeat after first 'parent_id'=> '-' it doesn't go to other parent_id would like to get any help with the explanations.

CodePudding user response:

This solution can help you.

function buildTree(array $elements, ?int $parentId = null): array
{
    $branch = [];
    foreach ($elements as $element) {
        if ($element['parent_id'] === $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[] = $element;
        }
    }

    return $branch;
}


$array = [
    ['id'=> 1, 'parent_id' => '-', 'name' => 'id1'],
    ['id' => 2, 'parent_id' => 1, 'name'=> 'id2'],
    ['id' => 3, 'parent_id' => 1, 'name'=> 'id3'],
    ['id' => 4, 'parent_id' => '-', 'name'=> 'id4'],
    ['id' => 5,'parent_id' => 2, 'name'=> 'id5'],
    ['id' => 6, 'parent_id' => 3, 'name'=> 'id6'],
    ['id' => 7, 'parent_id' => '-', 'name'=> 'id7'],
    ['id' => 8, 'parent_id' => 3, 'name'=> 'id8'],
    ['id' => 9, 'parent_id' => 4, 'name'=> 'id9'], 
    ['id' => 10, 'parent_id' => 9, 'name'=> 'id10'],
];

$tree = [];
foreach ($array as $element) {
    if ($element['parent_id'] === '-') {
        $tree[] = [
            'id' => $element['id'],
            'name' => $element['name'],
            'children' => buildTree($array, $element['id']),
        ];
    }
}

The print_r of the tree:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => id1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [parent_id] => 1
                            [name] => id2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 5
                                            [parent_id] => 2
                                            [name] => id5
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [parent_id] => 1
                            [name] => id3
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [parent_id] => 3
                                            [name] => id6
                                        )

                                    [1] => Array
                                        (
                                            [id] => 8
                                            [parent_id] => 3
                                            [name] => id8
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 4
            [name] => id4
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 9
                            [parent_id] => 4
                            [name] => id9
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 10
                                            [parent_id] => 9
                                            [name] => id10
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 7
            [name] => id7
            [children] => Array
                (
                )

        )

)

I put the code here if you want to test it: http://sandbox.onlinephpfunctions.com/code/ff22978371819db8e890a404e41b046bc613b7dd

I hope that I was useful. Good luck :)

CodePudding user response:

This line:

$tree = createTree($new, array($arr[0]));

only creates the list of descendants from arr[0].

To get the whole tree, you need to iterate through the list of root nodes:

foreach ($new['-'] as $root) {
$tree = createTree($new, array($root));
print_r($tree);
}

Teh playground

  • Related