php 如何合并数组并保留键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17462354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge array and preserve keys?
提问by Jasper
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:
我尝试使用联合运算符,但它不会覆盖该元素:
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?
我不想使用循环,有没有高性能的方法?
回答by Ja?ck
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:
您还可以使用联合数组运算符;它适用于旧版本,实际上也可能更快:
print_r($array2 + $array1);
回答by Orangepill
@Hyman 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
@Hyman 发现了可以执行此操作的本机函数,但由于它仅在 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;
}
}
回答by Starx
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));
回答by Mahesh Yadav
Let suppose we have 3 arrays as below.
假设我们有 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']);
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.
现在,如果您想合并所有这些数组并想要一个最终数组,该数组的所有数组数据都位于键 0 in 0 和 1 in 1 键下,依此类推。
Then you need to use array_replace_recursivePHP function, as below.
然后你需要使用array_replace_recursivePHP 函数,如下所示。
$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!!
希望上面的解决方案最适合您的要求!!
回答by TooTiredToDrink
I think this might help if i understand properly:
我认为如果我理解正确,这可能会有所帮助:
foreach ($i = 0, $num = count($array2); $i < $num; $i++)
{
$array = array_merge($array1, $arrar2[$i]);
}