I am having problems updating a deeply nested multidimensional php associative array. What I basically want it to add and 'parent_id' key with an incrementing value to all array elements that has an array under them.
For example i have the array below.
[root] =>
Array('child_1' =>
Array('child1_grandchild_1' => 'gchild1_value',
'child1_grandchild_2' => Array('grandchild_1' => 'gchildval1',
'grandchild_2 => 'gchildval2',
'grandchild_3' => 'gchildval3'),
'child1_grandchild_3' => 'gchild3_value'),
'child_2', => Array('child2_grandchild_1' => 'gchildval1',
'child2_grandchild_2' => 'gchildval2'),
'child_3' => 'child3_val',
'child_4' => 'child4_val'
);
I want to to add a parent key id element for elements with array values. Basically, the array above will transform into the array below. But I don't know how to do this considering I don't know how deeply nested the array is. I tried passing the array by reference by updating it doesn't work.
[root] =>
Array( 'parent_id' => 1
'child_1' =>
Array('child1_grandchild_1' => 'gchild1_value',
'child1_grandchild_2' => Array('parent_id' => 2,
'grandchild_1' => 'gchildval1',
'grandchild_2 => 'gchildval2',
'grandchild_3' => 'gchildval3'),
'child1_grandchild_3' => 'gchild3_value'),
'child_2', => Array('parent_id' => 3,
'child2_grandchild_1' => 'gchildval1',
'child2_grandchild_2' => 'gchildval2'),
'child_3' => 'child3_val',
'child_4' => 'child4_val'
);