0

I have a multidimensional array that i would like to go through, and add a new key and value to it called "depth" that holds an integer 0 and up. first level would be 0, a child array would hold 1, 2, and so on. The array can be in any format of children and continue chaining on endlessly. I was messing with some foreaching but was having trouble declaring deep key=>value pairs deeper in the array. I am looking to modify the arrays and add ['depth']=>.

the code i am messing with:

private function recursive($array, $level = 0, $parent = 0) {
    $newlevel = 0;
    $cototal = count($array);

    $arraybuild = array();
    foreach ($array as $key => $value) {

        if (is_numeric($key)) {
            if (is_array($value)) {
                $this->newtree[]=$value;
                $newlevel = $level+1;
                $this->recursive($value, $newlevel, $level);
            }
        }
    }
}

an example of the array is:

Array (
[0] => Array
(
    [name] => Whatwwww
    [cat_id] => 6
)

[1] => Array
(
    [name] => 43adsfasdfd
    [cat_id] => 5
    [children] => Array
    (
        [0] => Array
        (
            [name] => AAAAAAA
            [cat_id] => 7
            [children] => Array
            (
                [0] => Array
                (
                    [name] => CCCCCCCCCCCC
                    [cat_id] => 9
                )

            )

        )

        [1] => Array
        (
            [name] => bbbbbb
            [cat_id] => 8
        )

    )
)

[2] => Array
(
    [name] => Test Category
    [cat_id] => 1
    [children] => Array
    (
        [0] => Array
        (
            [name] => asdfasdfd
            [cat_id] => 4
        )

        [1] => Array
        (
            [name] => yetstes1
            [cat_id] => 2
        )
    )
)
)

modify the array to look something like this:

    Array (
    [0] => Array
    (
        [name] => Whatwwww
        [cat_id] => 6
        [depth] => 0
    )

    [1] => Array
    (
        [name] => 43adsfasdfd
        [cat_id] => 5
        [depth] => 0
        [children] => Array
        (
            [0] => Array
            (
                [name] => AAAAAAA
                [cat_id] => 7
                [depth] => 1
                [children] => Array
                (
                    [0] => Array
                    (
                        [name] => CCCCCCCCCCCC
                        [cat_id] => 9
                        [depth] => 2
                    )

                )

            )

            [1] => Array
            (
                [name] => bbbbbb
                [cat_id] => 8
                [depth] => 1
            )

        )
    )
ATMA
  • 129
  • 2
  • 6
  • Possible duplicate of [Is there a way to find out how deep a php array is](http://stackoverflow.com/questions/262891/is-there-a-way-to-find-out-how-deep-a-php-array-is) – larsAnders Apr 08 '16 at 01:12
  • I already know how to find how deep it is, i want to modify the deep nested arrays to add a key->value pair into that chunk with the depth – ATMA Apr 08 '16 at 01:31
  • So, to clarify, you would want that first element to end up like this: `[0] => Array ( [name] => Whatwwww [cat_id] => 6 [depth] => 1 )` right? – larsAnders Apr 08 '16 at 01:36
  • Ok, gotcha. Any way you can post that array as a usable variable, instead of all printed out? Like `$array = array(....etc...);` – larsAnders Apr 08 '16 at 01:38
  • Still need a hand on this, or you got it? – larsAnders Apr 08 '16 at 04:19

1 Answers1

1

Try this out. You'll need to adapt a bit to your specific codebase, but this works:

function addDepth_r(&$array, $level = 0) {

    foreach ($array as $k => $v) {      

        if (is_array($array[$k])) {
            $level++;
            addDepth_r($array[$k], $level);
        } else if(!isset($array['depth'])) {
            $array['depth'] = $level;
        }
    }
}

$a = array(

    array(
        'name' => 'Whatwwww',
        'cat_id' => 6
    ),

    array(
        'name' => '43adsfasdfd',
        'cat_id' => 5,
        'children' => array(
            array(
                'name' => 'AAAAAAA',
                'cat_id' => 7,
                'children' => array(
                    array(
                        'name' => 'CCCCCCCCCCCC',
                        'cat_id' => 9,
                    )

                )

            ),

            array(
                'name' => 'bbbbbb',
                'cat_id' => 8,
            )

        )
    )
);

echo '<pre>Initial array:<br>';
print_r($a);
echo '<br><br><br>Processed array:<br>';
addDepth_r($a);
print_r($a);
echo '</pre>';

If you want every level marked with depth, just rearrange the function:

function addDepth_r(&$array, $level = 0) {

    foreach ($array as $k => $v) {      
        if(!isset($array['depth'])) {
            $array['depth'] = $level;
        }
        if (is_array($array[$k])) {
            $level++;
            addDepth_r($array[$k], $level);
        }
    }
}
larsAnders
  • 3,813
  • 1
  • 15
  • 19