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
                                 )
Nibboro
  • 49
  • 4
  • 2
    What have you tried to get this done? It should not be that difficult – Nico Haase Apr 03 '18 at 08:43
  • You can check there :- https://stackoverflow.com/questions/8587341/recursive-function-to-generate-multidimensional-array-from-database-result/8587437#8587437 – nageen nayak Apr 03 '18 at 08:57

3 Answers3

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);
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
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);
nageen nayak
  • 1,262
  • 2
  • 18
  • 28
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();
Ramy Herrira
  • 574
  • 10
  • 13