How to 'merge' some array elements into a element of the same array?

Ijwbywcwz

New Member
\[code\]$foo = array( '1' => '2', '3' => array( '4' => '5' ), '6' => array( '7' => '8', '9' => '10', '11' => array( '12' => '13', '14' => '15' ) ));$bar = array( '1', '6' => array( '7', '11' => array( '12' ) ));\[/code\]Foo is an array i have to edit, Bar the edits i need to do.I have to create another element in Foo array containing the elements pointed in Bar, and delete the originals from Foo.So, with the array, the final array should be:\[code\]Array( '3' => array( '4' => '5' ), '6' => array( '9' => '10', '11' => array( '14' => '15' ) ), 'merged' => array( '1' => '2', '6' => array( '7' => '8', '11' => array( '12' => '13' ) ) ))\[/code\]I've build this recursive function, but works only for the first level of the array:\[code\]foreach($bar AS $key => $value){ if(is_array($value)){ s($foo, $key, $value); }else{ $foo['merged'][$value] = $foo[$value]; unset($foo[$value]); }}function s(&$form, $key, $value){ if(is_array($value)){ foreach($value AS $k => $v){ s($form, $k, $v); } }else{ $form['merged'][$value] = $form[$value]; unset($foo[$value]); }}\[/code\]Any ideas?
 
Back
Top