带数字键的 PHP array_merge
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5929642/
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
PHP array_merge with numerical keys
提问by Alex
How can make it so array_merge() overwrites two keys with different values but same key index from two arrays?
如何使 array_merge() 覆盖两个数组中具有不同值但具有相同键索引的两个键?
for example, merging:
例如,合并:
[0] => 'whatever'
[0] => 'whatever'
with
和
[0] => 'whatever', [1] => 'a', [2] => 'b'
[0] => 'whatever', [1] => 'a', [2] => 'b'
should produce
应该产生
[0] => 'whatever', [1] => 'a', [2] => 'b'
[0] => 'whatever', [1] => 'a', [2] => 'b'
Basically I want array_merge to bahave the same way it behaves if the arrays have string keys...
基本上,如果数组具有字符串键,我希望 array_merge 具有相同的行为方式......
回答by AJ.
Use the +
operator.
使用+
运算符。
Compare array_merge
to +
operator:
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);
?>
Output:
输出:
Array
(
[0] => whatever
[1] => whatever
[2] => a
[3] => b
)
Array
(
[0] => whatever
[1] => a
[2] => b
)
The +
operator stillworks if your associative array has the numerical keys out-of-order:
如果关联数组的数字键乱序,+
运算符仍然有效:
<?php
$a1 = array(0=>"whatever",);
$a2 = array(1=>"a",0=>"whatever",2=>"b");
print_r(array_merge($a1,$a2));
print_r($a1+$a2);
?>
Output:
输出:
Array
(
[0] => whatever
[1] => a
[2] => whatever
[3] => b
)
Array
(
[0] => whatever
[1] => a
[2] => b
)
Notice array_merge
in thiscase creates a new key. Not desirable...
请注意,array_merge
在这种情况下会创建一个新密钥。不可取...
回答by HKandulla
array_replace
does exactly this!
array_replace
正是这样做的!
回答by Jon
Pretty easy to write manually:
手动编写非常容易:
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;
}
Update:This behaves differently than the union operator (return $first + $second;
) because in this case the secondarray wins when both have elements with the same key.
更新:这与联合运算符 ( return $first + $second;
) 的行为不同,因为在这种情况下,当两个数组都具有相同键的元素时,第二个数组将获胜。
However, if you switch the places of the arguments and place the array that you want to "win" in case of conflicts as the firstoperand, you can get the same behavior. So the function above behaves exactly like return $second + $first;
.
但是,如果您切换参数的位置并将发生冲突时想要“获胜”的数组作为第一个操作数,您可以获得相同的行为。所以上面的函数的行为与return $second + $first;
.
回答by Manvel
In my project I use my own function
在我的项目中我使用我自己的函数
function array_merge_custom(){
$array = [];
$arguments = func_num_args();
foreach($arguments as $args)
foreach($args as $key => $value)
$array[$key] = $value;
return $array;
}
Usage
用法
$a = array_merge_custom($b, $c, $d, ... .. )
回答by G?ktu? ?ztürk
You should use $a2+$a1
to get same result with array_merge($a1,$a2);
你应该用$a2+$a1
得到相同的结果array_merge($a1,$a2);
$a1 = array(
'k1' => 1,
'k2' => 2,
'k3' => 3,
);
$a2 = array(
'k1' => 11,
'k2' => 22,
'k4' => 44,
);
Code:
代码:
print_r(array_merge($a1,$a2));
Output:
输出:
Array (
[k1] => 11
[k2] => 22
[k3] => 3
[k4] => 44
)
Code:
代码:
print_r($a1+$a2);
Output:
输出:
Array (
[k1] => 1
[k2] => 2
[k3] => 3
[k4] => 44
)
Code:
代码:
print_r($a2+$a1);
Output:
输出:
Array (
[k1] => 11
[k2] => 22
[k4] => 44
[k3] => 3
)
回答by phvish
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));
回答by Scott C Wilson
You could use array_merge()
and then use array_unique()
.
您可以使用array_merge()
然后使用array_unique()
.
回答by Sergey Onishchenko
$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
)
Compare to WRONGuse of "+"
与“+”的错误使用相比较
$arrCommon = $arrA + $arrB;
print_r($arrCommon);
Array
(
[0] => 10
[1] => 11
[2] => 12
)