php 合并两个数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6535444/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:40:10  来源:igfitidea点击:

Combine two arrays

phparraysunique

提问by Awan

I have two arrays like this:

我有两个这样的数组:

array( 
'11' => '11',
'22' => '22',
'33' => '33',
'44' => '44'
);

array( 
'44' => '44',
'55' => '55',
'66' => '66',
'77' => '77'
);

I want to combine these two array such that it does not contains duplicate and as well as keep their original keys. For example output should be:

我想组合这两个数组,使其不包含重复项并保留其原始键。例如输出应该是:

array( 
'11' => '11',
'22' => '22',
'33' => '33',
'44' => '44',
'55' => '55',
'66' => '66',
'77' => '77'
);

I have tried this but it is changing their original keys:

我试过这个,但它正在改变他们的原始密钥:

$output = array_unique( array_merge( $array1 , $array2 ) );

Any solution?

有什么解决办法吗?

回答by KingCrunch

Just use:

只需使用:

$output = array_merge($array1, $array2);

That should solve it. Because you use string keys if one key occurs more than one time (like '44'in your example) one key will overwrite proceding ones with the same name. Because in your case they both have the same value anyway it doesn't matter and it will also remove duplicates.

那应该可以解决它。因为如果一个键出现多次(如'44'您的示例中),则您使用字符串键,所以一个键将覆盖具有相同名称的后续键。因为在您的情况下,它们无论如何都具有相同的值,这无关紧要,并且还会删除重复项。

Update: I just realised, that PHP treats the numeric string-keys as numbers (integers) and so will behave like this, what means, that it renumbers the keys too...

更新:我刚刚意识到,PHP 将数字字符串键视为数字(整数),因此会表现得像这样,这意味着它也会对键重新编号...

A workaround is to recreate the keys.

解决方法是重新创建密钥。

$output = array_combine($output, $output);

Update 2: I always forget, that there is also an operator (in bold, because this is reallywhat you are looking for! :D)

更新 2:我总是忘记,还有一个运算符(以粗体显示,因为这确实是您要查找的内容!:D)

$output = $array1 + $array2;

All of this can be seen in: http://php.net/manual/en/function.array-merge.php

所有这些都可以在:http: //php.net/manual/en/function.array-merge.php 中看到

回答by inemanja

You should take to consideration that $array1 + $array2 != $array2 + $array1

你应该考虑到 $array1 + $array2 != $array2 + $array1

$array1 = array(
'11' => 'x1',
'22' => 'x1' 
);  

$array2 = array(
'22' => 'x2',
'33' => 'x2' 
);

with $array1+ $array2

$array1+ $array2

$array1 + $array2 = array(
'11' => 'x1',
'22' => 'x1',
'33' => 'x2'
);

and with $array2+ $array1

$array2+ $array1

$array2 + $array1 = array(  
'11' => 'x1',  
'22' => 'x2',  
'33' => 'x2'  
);

回答by Michas

This works:

这有效:

$output = $array1 + $array2;

回答by Brendan Bullen

To do this, you can loop through one and append to the other:

为此,您可以遍历一个并附加到另一个:

<?php

$test1 = array( 
'11' => '11',
'22' => '22',
'33' => '33',
'44' => '44'
);

$test2 = array( 
'44' => '44',
'55' => '55',
'66' => '66',
'77' => '77'
);


function combineWithKeys($array1, $array2)
{
    foreach($array1 as $key=>$value) $array2[$key] = $value;
    asort($array2);
    return $array2;
} 

print_r(combineWithKeys($test1, $test2));

?>

UPDATE:KingCrunch came up with the best solution: print_r($array1+$array2);

更新:KingCrunch 提出了最佳解决方案print_r($array1+$array2);

回答by Student of Science

If you are using PHP 7.4 or above, you can use the spread operator ...as the following examples from the PHP Docs:

如果您使用的是 PHP 7.4 或更高版本,则可以使用扩展运算符...作为 PHP 文档中的以下示例:

$arr1 = [1, 2, 3];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
$arr4 = array(...$arr1, ...$arr2, 111); //[1, 2, 3, 1, 2, 3, 111]
$arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3]

function getArr() {
  return ['a', 'b'];
}
$arr6 = [...getArr(), 'c']; //['a', 'b', 'c']

$arr7 = [...new ArrayIterator(['a', 'b', 'c'])]; //['a', 'b', 'c']

function arrGen() {
    for($i = 11; $i < 15; $i++) {
        yield $i;
    }
}
$arr8 = [...arrGen()]; //[11, 12, 13, 14]

It works like in JavaScript ES6.

它的工作原理类似于 JavaScript ES6。

See more on https://wiki.php.net/rfc/spread_operator_for_array.

https://wiki.php.net/rfc/spread_operator_for_array上查看更多信息。

回答by Norman Edance

Warning! $array1 + $array2overwrites keys, so my solution (for multidimensional arrays) is to use array_unique()

警告!$array1 + $array2覆盖键,所以我的解决方案(对于多维数组)是使用array_unique()

array_unique(array_merge($a, $b), SORT_REGULAR);

Notice:

注意:

5.2.10+Changed the default value of sort_flagsback to SORT_STRING.

5.2.9Default is SORT_REGULAR.

5.2.8-Default is SORT_STRING

5.2.10+将默认值改sort_flags回 SORT_STRING。

5.2.9默认为 SORT_REGULAR。

5.2.8-默认为 SORT_STRING

It perfectly works. Hope it helps same.

完美地工作。希望它有帮助。

回答by jeni

This works:

这有效:

$a = array(1 => 1, 2 => 2, 3 => 3);
$b = array(4 => 4, 5 => 5, 6 => 6);
$c = $a + $b;
print_r($c);

回答by Shoaib Ahmed

The new way of doing it with php7.4is Spread operator [...]

使用php7.4的新方法是 Spread 运算符[...]

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
var_dump($fruits);

Spread operator should have better performance than array_merge

扩展运算符应该比 array_merge

A significant advantage of Spread operator is that it supports any traversable objects, while the array_merge function only supports arrays.

Spread 运算符的一个显着优点是它支持任何可遍历的对象,而 array_merge 函数仅支持数组。

回答by Rafi Ullah Patel

https://www.php.net/manual/en/function.array-merge.php

https://www.php.net/manual/en/function.array-merge.php

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>