0

I have an multidimensional array and I want to add a key and value to every array within the multidimensional array. The value has to be the level of how deep the array is within the multidimensional array.

For example:

Array(
    [0] Array
        (
            [id] 1
            [parentid] null
            [0] Array
                (
                    [id] 101
                    [parentid] 1
    [1] Array
        (
            [id] 2
            [parentid] null
            [0] Array
                (
                    [id] 161
                    [parentid] 2
                    [children] Array
                        (
                            [0] Array
                                (
                                    [id] 300
                                    [parentid] 161
                                 )

Expected output:

Array(
    [0] Array
        (
            [id] 1
            [parentid] null
            [level] 1
            [0] Array
                (
                    [id] 101
                    [parentid] 1
                    [level] 2
    [1] Array
        (
            [id] 2
            [parentid] null
            [level] 1
            [0] Array
                (
                    [id] 161
                    [parentid] 2
                    [level] 2
                    [children] Array
                        (
                            [0] Array
                                (
                                    [id] 300
                                    [parentid] 161
                                    [level] 3
                                 )
2
  • 2
    What have you tried to get this done? It should not be that difficult Commented Apr 3, 2018 at 8:43
  • You can check there :- stackoverflow.com/questions/8587341/… Commented Apr 3, 2018 at 8:57

3 Answers 3

2

You basically want to use a recursive function that works with a reference to an array.

By using a parameter &$foo with the & in front of it, you're designating it as a reference to that object.

In the case of an array it will not make a copy of the modification, but perform the modification on the original passed array.

EDIT added suggestion of Yoshi in the comments to pass &value as reference too.

See it live: https://ideone.com/NhKABF

<?php
$array = [
    'hello' => 'world',
    'doing' => [
        'hello' => 'universe',
        'going' => [
            'hello' => 'existence'
         ],
        'moving' => [
            'answer' => 42,
        ]
    ]
];
function levelUp(&$array, $level = 1) 
//               ^-- See that one? that's the  magic.
{
    $array['level'] = $level;

    foreach($array as $key => &$value) {
    //                        ^-- important to add that & here too
        if(is_array($value)) {
            levelUp($value, $level + 1);
        }
    }
}
levelUp($array);
var_dump($array);
Sign up to request clarification or add additional context in comments.

2 Comments

Small thing: I think the OP wants the level field set to the data's nesting level, not the id field. Otherwise great answer.
@Nibboro is given solution solved your problem
0
function buildTree(array $elements, $parentId = 0) {
$branch = array();

foreach ($elements as $element) {
    if ($element['parent_id'] == $parentId) {
        $children = buildTree($elements, $element['id']);
        if ($children) {
            $element['children'] = $children;
        }
        $branch[] = $element;
    }
}

return $branch;

}

$tree = buildTree($rows);

8 Comments

I think it will help you.
Obviously, because otherwise you wouldn't have posted it ;)
I use this function to build my tree structure. But I've no idea how to add the current level of the array to the array.
Have you store level in DB??
No, the level is not stored in the database yet. Im trying to add the level in the array and want to use that array to insert the data in the database.
|
0

I don't know if it's relevant but why not use a library like BlueM/Tree ?
It has features built for this kind of problems.

// Get a node's ID
$id = $node->getId();

// Get the node's hierarchical level (1-based)
$level = $node->getLevel();

1 Comment

May be but i have not use yet that library.

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.