如何使它成为array_merge()用两个数组覆盖具有不同值但相同键索引的两个键?
例如,合并:
[0] => 'whatever'
与
[0] => 'whatever', [1] => 'a', [2] => 'b'
应该产生
[0] => 'whatever', [1] => 'a', [2] => 'b'
基本上我希望array_merge与数组有字符串键时的行为方式相同...
答案 0 :(得分:72)
使用+
运算符。
将array_merge
与+
运算符进行比较:
<?php
$a1 = array(0=>"whatever",);
$a2 = array(0=>"whatever",1=>"a",2=>"b");
print_r(array_merge($a1,$a2));
print_r($a1+$a2);
?>
输出:
Array
(
[0] => whatever
[1] => whatever
[2] => a
[3] => b
)
Array
(
[0] => whatever
[1] => a
[2] => b
)
如果关联数组的数字键无序,则+
运算符仍然有效:
<?php
$a1 = array(0=>"whatever",);
$a2 = array(1=>"a",0=>"whatever",2=>"b");
print_r(array_merge($a1,$a2));
print_r($a1+$a2);
?>
输出:
Array
(
[0] => whatever
[1] => a
[2] => whatever
[3] => b
)
Array
(
[0] => whatever
[1] => a
[2] => b
)
注意此案例中的array_merge
会创建一个新密钥。不可取......
答案 1 :(得分:7)
手动编写非常简单:
function array_merge_custom($first, $second) {
$result = array();
foreach($first as $key => $value) {
$result[$key] = $value;
}
foreach($second as $key => $value) {
$result[$key] = $value;
}
return $result;
}
更新:这与union运算符(return $first + $second;
)的行为不同,因为在这种情况下, second 数组在两个元素具有相同键时都会获胜。
但是,如果切换参数的位置并将要“赢”的数组放在第一个操作数的冲突情况下,则可以获得相同的行为。因此,上述函数的行为与return $second + $first;
完全相同。
答案 2 :(得分:6)
array_replace
就是这样做的。请参阅:http://php.net/manual/de/function.array-replace.php
答案 3 :(得分:3)
在我的项目中,我使用自己的功能
function array_merge_custom(){
$array = [];
$arguments = func_num_args();
foreach($arguments as $args)
foreach($args as $key => $value)
$array[$key] = $value;
return $array;
}
用法
$a = array_merge_custom($b, $c, $d, ... .. )
答案 4 :(得分:3)
您应该使用$a2+$a1
与array_merge($a1,$a2);
$a1 = array(
'k1' => 1,
'k2' => 2,
'k3' => 3,
);
$a2 = array(
'k1' => 11,
'k2' => 22,
'k4' => 44,
);
<强>代码:强>
print_r(array_merge($a1,$a2));
输出
Array (
[k1] => 11
[k2] => 22
[k3] => 3
[k4] => 44
)
<强>代码:强>
print_r($a1+$a2);
输出
Array (
[k1] => 1
[k2] => 2
[k3] => 3
[k4] => 44
)
<强>代码:强>
print_r($a2+$a1);
输出
Array (
[k1] => 11
[k2] => 22
[k4] => 44
[k3] => 3
)
答案 5 :(得分:0)
您可以使用array_merge()
,然后使用array_unique()
。
答案 6 :(得分:0)
the solution could be this:
function array_merge_custom($array1, $array2) {
$mergeArray = [];
$array1Keys = array_keys($array1);
$array2Keys = array_keys($array2);
$keys = array_merge($array1Keys, $array2Keys);
foreach ($keys as $key) {
$mergeArray[$key] = array_merge_recursive(isset($array1[$key]) ? $array1[$key] : [], isset($array2[$key]) ? $array2[$key] : []);
}
return $mergeArray;
}
$array1 = [
'66_' => [
'k1' => 1,
'k2' => 1,
],
'67_' => [
'k1' => 1,
'k2' => 1,
],
'68_' => [
'k1' => 1,
'k2' => 1,
],
68 => [
'k1' => 1,
'k2' => 1,
]
];
$array2 = [
'66_' => [
'a1' => 1,
'a2' => 1,
],
'68_' => [
'b1' => 1,
'b2' => 1,
],
68 => [
'b1' => 1,
'b2' => 1,
]
];
echo '<pre>';
print_r(array_merge_custom($array1, $array2));
答案 7 :(得分:-1)
$arrA = [10, 11, 12];
$arrB = [12, 13];
$arrCommon = array_keys(array_flip($arrA) + array_flip($arrB));
print_r($arrCommon);
Array
(
[0] => 10
[1] => 11
[2] => 12
[3] => 13
)
与错误使用&#34; +&#34;
进行比较$arrCommon = $arrA + $arrB;
print_r($arrCommon);
Array
(
[0] => 10
[1] => 11
[2] => 12
)