Merge or merge an array like this? in PHP

I have a multidimensional PHP array (below).


Array ( [0] => Array ( [2] => one@example.com ) [1] => Array ( [4] => two@example.com ) [2] => Array ( [3908] => twenty@example.com ) [3] => Array ( [2548] => eleven@example.com ) [4] => Array ( [3099] => ten@example.com ) [5] => Array ( [5283] => six@example.com ) ) 

I was wondering how can I merge? or combine? or just do it like this using PHP (below).


 Array ( [2] => one@example.com [4] => two@example.com [3908] => twenty@example.com [2548] => eleven@example.com [3099] => ten@example.com [5283] => six@example.com ) 

reference

+4
source share
3 answers

You can β€œpromote” second-level elements to first-level elements through call_user_func_array () . And while array_merge () re-indexes the array, array_replace () does not save the source keys, which make it single-line:

 $a = call_user_func_array('array_replace', $a); 

Test:

 <?php $a = array ( array (2 => ' one@example.com '), array (4 => ' two@example.com '), array (3908 => ' twenty@example.com '), array (2548 => ' eleven@example.com '), array (3099 => ' ten@example.com '), array (5283 => ' six@example.com '), ); $a = call_user_func_array('array_replace', $a); var_dump($a); 
+3
source

Just go through the array and save it to another.

 $a = array( 0 => array(1) , 1=> array(3) ); $another=array(); foreach ($a as $k=>$v){ foreach ($v as $y=>$z){ $another[$k]=$z; } } print_r($another); 
0
source

How about this?

 foreach( $old_arr as $old ) { $key = key( $old[0] ); $new[ $key ] = $old[0][$key]; } 

EDIT: I never thought the previous answer was fixed.

-1
source

All Articles