PHP-如何合并数组和保留键?

我有两个数组:

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

我想合并它们并保留键和顺序,而不是重新索引!

怎么会这样?

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

我尝试使用array_merge(),但不会保留键:

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

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

我尝试使用联合运算符,但它不会覆盖该元素:

print_r($array1 + $array2);

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

我尝试交换位置,但顺序错误,不是我的需要:

print_r($array2 + $array1);

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

我不想使用循环,有没有一种高性能的方法?

Jasper asked 2019-11-15T17:55:10Z
5个解决方案
136 votes

您正在寻找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));

自PHP 5.3起可用。

更新

您也可以使用联合数组运算符; 它适用于旧版本,并且实际上可能也更快:

print_r($array2 + $array1);
Ja͢ck answered 2019-11-15T17:55:47Z
4 votes

@Jack发现了可以执行此操作的本机功能,但由于它仅在php 5.3及更高版本中可用,因此应该可以在5.3之前的安装中模拟此功能

  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;
     }
 }
Orangepill answered 2019-11-15T17:56:11Z
2 votes

array_replace_recursive()array_replace()是您要寻找的功能

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

演示

Starx answered 2019-11-15T17:56:37Z
1 votes

假设我们有3个数组,如下所示。

$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']); 

现在,如果要合并所有这些数组,并希望最终数组的所有数据都位于键0中的0和键1中1的最终数组中,依此类推。

然后,您需要使用array_replace_recursive PHP函数,如下所示。

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

结果如下。

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
        )

)

希望以上解决方案最适合您的要求!!

Mahesh Yadav answered 2019-11-15T17:57:27Z
-2 votes

我认为如果我理解正确,这可能会有所帮助:

foreach ($i = 0, $num = count($array2); $i < $num; $i++)
{
  $array = array_merge($array1, $arrar2[$i]);
}
TooTiredToDrink answered 2019-11-15T17:57:52Z
translate from https://stackoverflow.com:/questions/17462354/how-to-merge-array-and-preserve-keys