0

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.

4
  • 2
    We now know what you are trying to achieve, but we do not know what your problem is. I take it, you are not getting the output you want, but what is it you are getting? We are willing to help you, but we can only do so, if you tell us precisely about the problem you are having. Commented Nov 19, 2021 at 12:45
  • so I want to make a function which gets all the data from $tree and make it back like $array so if anyone make the function and explain a bit would really helpful and my function right now only makes the array with information titles like = 'id' but doesn't get the values from those keys @RefugnicEternium my function is not in the post the function in the post make $array in to $tree Commented Nov 19, 2021 at 12:55
  • As I said before, we are happy to help you if you have a specific problem, but I'm afraid we're not supposed to do (all) your work for you. I understand that coding homework (cause that's what this feels like), can sometimes be difficult, but there's no benefit for you if someone else does it for you. Commented Nov 19, 2021 at 13:00
  • @RefugnicEternium I will post my codes after I finish with them no worries if you don't feel like it. Commented Nov 19, 2021 at 13:11

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

now I know how to use ?? I was bit start of the function trying to do if($array['parent_id'] != null) { $parentId = $array['parent_id']; } else { $parentId = 0; } but if you can explain the 88-89 lines ?@Lenny4
This line sort the elements by id. php.net/manual/en/function.usort.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.