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
            )

        )
    )
5
  • Possible duplicate of Is there a way to find out how deep a php array is Commented Apr 8, 2016 at 1: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 Commented Apr 8, 2016 at 1: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? Commented Apr 8, 2016 at 1:36
  • Ok, gotcha. Any way you can post that array as a usable variable, instead of all printed out? Like $array = array(....etc...); Commented Apr 8, 2016 at 1:38
  • Still need a hand on this, or you got it? Commented Apr 8, 2016 at 4:19

1 Answer 1

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

Comments

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.