如何合并数组和保存键?

时间:2021-07-05 12:18:12

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:

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

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:

我尝试给union操作符,但它不会覆盖这个元素:

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?

我不想使用循环,有什么方法可以提高性能吗?

4 个解决方案

#1


109  

You're looking for array_replace():

你正在寻找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.

自PHP 5.3可用。

Update

更新

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

您还可以使用union数组操作符;它适用于旧版本,而且实际上可能更快:

print_r($array2 + $array1);

#2


3  

@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

@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;
     }
 }

#3


2  

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

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

Demo

演示

#4


-2  

I think this might help if i understand properly:

如果我能正确理解的话,我想这可能会有帮助。

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

#1


109  

You're looking for array_replace():

你正在寻找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.

自PHP 5.3可用。

Update

更新

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

您还可以使用union数组操作符;它适用于旧版本,而且实际上可能更快:

print_r($array2 + $array1);

#2


3  

@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

@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;
     }
 }

#3


2  

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

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

Demo

演示

#4


-2  

I think this might help if i understand properly:

如果我能正确理解的话,我想这可能会有帮助。

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