How to merge array and preserve keys?

PhpArrays

Php Problem Overview


I have two arrays:

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);

I want to merge them and keep the keys and the order and not re-index!!

How to get like this?

Array
(
    [a] => new value
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 5
    [f] => 6
    [123] => 456
)

I try to array_merge() but it will not be preserved the keys:

print_r(array_merge($array1, $array2));

Array
(
    [a] => 'new value'
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 5
    [f] => 6
    [0] => 456
)

I try to the union operator but it will not overwriting that element:

print_r($array1 + $array2);

Array
(
    [a] => 1   <-- not overwriting
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 5
    [f] => 6
    [123] => 456
)

I try to swapped place but the order is wrong, not my need:

print_r($array2 + $array1);

Array
(
    [d] => 4
    [e] => 5
    [f] => 6
    [a] => new value 
    [123] => 456
    [b] => 2
    [c] => 3
)

I dont want to use a loop, is there a way for high performance?

Php Solutions


Solution 1 - Php

You're looking for array_replace():

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
print_r(array_replace($array1, $array2));

Available since PHP 5.3.

Update

You can also use the union array operator; it works for older versions and might actually be faster too:

print_r($array2 + $array1);

Solution 2 - Php

@Jack uncovered the native function that would do this but since it is only available in php 5.3 and above this should work to emulate this functionality on pre 5.3 installs

  if(!function_exists("array_replace")){
      function array_replace(){
         $args = func_get_args();
         $ret = array_shift($args);
         foreach($args as $arg){
             foreach($arg as $k=>$v){
                $ret[(string)$k] = $v;
             }
         }
         return $ret;
     }
 }

Solution 3 - Php

Let suppose we have 3 arrays as below.

$a = array(0=>['label'=>'Monday','is_open'=>1],1=>['label'=>'Tuesday','is_open'=>0]);

$b = array(0=>['open_time'=>'10:00'],1=>['open_time'=>'12:00']); 

$c = array(0=>['close_time'=>'18:00'],1=>['close_time'=>'22:00']); 

Now, if you want to merge all these array and want a final array that have all array's data under key 0 in 0 and 1 in 1 key as so on.

Then you need to use array_replace_recursive PHP function, as below.

$final_arr = array_replace_recursive($a, $b , $c); 

The result of this will be as below.

Array
(
    [0] => Array
        (
            [label] => Monday
            [is_open] => 1
            [open_time] => 10:00
            [close_time] => 18:00
        )

    [1] => Array
        (
            [label] => Tuesday
            [is_open] => 0
            [open_time] => 12:00
            [close_time] => 22:00
        )

)

Hope the solution above, will best fit your requirement!!

Solution 4 - Php

array_replace_recursive() or array_replace() is the function you are looking for

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
 
 
var_dump(array_replace_recursive($array1, $array2));

Demo

Solution 5 - Php

I think this might help if i understand properly:

foreach ($i = 0, $num = count($array2); $i < $num; $i++)
{
  $array = array_merge($array1, $arrar2[$i]);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJasperView Question on Stackoverflow
Solution 1 - PhpJa͢ckView Answer on Stackoverflow
Solution 2 - PhpOrangepillView Answer on Stackoverflow
Solution 3 - PhpMahesh YadavView Answer on Stackoverflow
Solution 4 - PhpStarxView Answer on Stackoverflow
Solution 5 - PhpdsumaView Answer on Stackoverflow