Home > Mobile >  function to make Tree like Array in to Array
function to make Tree like Array in to Array

Time:11-19

I have $tree like array which made by function from the $array I need a function to make it $tree to $array back so I need a function to make it back. lets call it $list this time.

I will share $tree , $array and the function in below

tree like array ;

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

        )

)

Which made by a function from this array

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

function(making $array in to $tree)

    $tree = [];
function buildTree (array $infos, int $parent_Id = null): array
{
    $branch = [];
    foreach ($infos as $info)
    if($info['parent_id'] === $parent_Id){
        $children = buildTree($infos , $info['id']);
        if ($children){
           $info['children'] = $children;
        }
        $branch[] = $info;
    }
     return $branch;
}
foreach ($array as $info){
    if($info['parent_id']=== 0){
        $tree[] = [
            'id' => $info['id'],
            'name' => $info['name'],
            'children' => buildTree($array , $info['id']),
        ];
    }

}
print_r($tree);

any explanation would be appreciated.

CodePudding user response:

Result of this code can be found here: http://sandbox.onlinephpfunctions.com/code/38a091db5ace63900fa0bf69ddde17412118513c

function flatMergeArray(array $array, int $parentId = 0, array &$result = []): array
{
    $subResult = [];
    foreach ($array as $key => $sub) {
        $parentId = $array['parent_id'] ?? 0;
        if (is_array($sub)) {
            flatMergeArray($sub, $parentId, $result);
        } else {
            $subResult[$key] = $sub;
        }
    }
    if (!empty($subResult)) {
        if (!isset($subResult['parent_id'])) {
            $subResult['parent_id'] = 0;
        }
        $result[] = $subResult;
    }
    return $result;
}

function flatTree(array $tree): array
{
    $array = flatMergeArray($tree);
    usort($array, static function (array $node1, array $node2) {
        return ($node1['id'] < $node2['id']) ? -1 : 1;
    });
    return array_values($array);
}

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

$array = flatTree($tree);

print_r($array);
  • Related