1

How can I turn this flat array into a multi-dimensional array according to the matched key?

$items = [
    0 => [
        'id' => 100,
        'parent' => 0,
        'url' => 'Home'
    ],

    1 => [
        'id' => 101,
        'parent' => 0,
        'url' => 'About'
    ],

    2 => [
        'id' => 102,
        'parent' => 101,
        'url' => 'Group'
    ],

    3 => [
        'id' => 103,
        'parent' => 102,
        'url' => 'Mission'
    ],
    4 => [
        'id' => 104,
        'parent' => 102,
        'url' => 'Vision'
    ],
];

My attempt:

$new_items = array();
foreach ($items as $key => $item) {

    // Store what you need.
    $temp_item = array(
        'id' => $item['id'],
        'url' => $item['url'],
        'parent_id' => $item['parent'],
        'children' => array()
    );

    // Item does not have a parent so item_parent equals 0 (false).
    if (!$item['parent']) {
        // Push the item to the array.
        array_push($new_items, $temp_item);
    }

    // Item that has a parent.
    if ($item['parent']) {

        // Search key by column 'id'.
        $key = array_search($item['parent'], array_column($new_items, 'id'));

        // Push sub item to the children array.
        array_push($new_items[$key]['children'], $temp_item);
    }
}

print_r($new_items);

Result:

Array
(
    [0] => Array
        (
            [id] => 100
            [url] => Home
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 103
                            [url] => Mission
                            [parent_id] => 102
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 104
                            [url] => Vision
                            [parent_id] => 102
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 101
            [url] => About
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 102
                            [url] => Group
                            [parent_id] => 101
                            [children] => Array
                                (
                                )

                        )

                )

        )

)

This is what I am after:

Array
(
    [0] => Array
        (
            [id] => 100
            [url] => Home
            [parent_id] => 0
            [children] => Array
                (
                )

        )

    [1] => Array
        (
            [id] => 101
            [url] => About
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 102
                            [url] => Group
                            [parent_id] => 101
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 103
                                            [url] => Mission
                                            [parent_id] => 102
                                            [children] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 104
                                            [url] => Vision
                                            [parent_id] => 102
                                            [children] => Array
                                                (
                                                )

                                        )
                                )

                        )

                )

        )

)

Any ideas and suggestions?

1 Answer 1

2

From link

I have changed function parameters as per requirement.
Here is your code.

function buildTree(array $elements, $options = [
    'parent_id_column_name' => 'parent',
    'children_key_name' => 'children',
    'id_column_name' => 'id'], $parentId = 0)
    {
    $branch = array();
    foreach ($elements as $element) {
        if ($element[$options['parent_id_column_name']] == $parentId) {
            $children = buildTree($elements, $options, $element[$options['id_column_name']]);
            if ($children) {
                $element[$options['children_key_name']] = $children;
            }else{
                $element[$options['children_key_name']] = []; // added this line for empty children array
            }
            $branch[] = $element;
        }
    }
    return $branch;
}

Here is your working demo.

Sign up to request clarification or add additional context in comments.

1 Comment

It is optimized but yeah robust with n-levels

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.