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
)
)
)