php array_merge 和 array_unique
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4660675/
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
array_merge & array_unique
提问by ptmr.io
Is there an array function in PHP that somehow does array_merge, comparing the values, ignoring the keys? I think that array_unique(array_merge($a, $b))
works, however I believe there must be a nicer way to do this.
PHP 中是否有一个数组函数以某种方式执行 array_merge,比较值,忽略键?我认为这是array_unique(array_merge($a, $b))
可行的,但是我相信必须有更好的方法来做到这一点。
eg.
例如。
$a = array(0 => 0, 1 => 1, 2 => 2);
$b = array(0 => 2, 1 => 3, 2 => 4);
resulting in:
导致:
$ab = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4);
Please note that I don't care about the keys in $ab
, however it would be niceif they were ascending, starting at 0 to count($ab)-1
.
请注意,我不关心的按键$ab
,但它会很高兴,如果他们上升,从0开始到count($ab)-1
。
回答by orrd
The most elegant, simple, and efficient solution is the one mentioned in the original question...
最优雅、最简单、最有效的解决方案是原始问题中提到的那个...
$ab = array_unique(array_merge($a, $b));
This answer was also previously mentioned in comments by Ben Lee and doublejosh, but I'm posting it here as an actual answer for the benefit of other people who find this question and want to know what the best solution is without reading all the comments on this page.
Ben Lee 和 doublejosh 之前在评论中也提到了这个答案,但我将它发布在这里作为实际答案,以供其他发现此问题并想知道最佳解决方案的人受益,而无需阅读所有评论这一页。
回答by Oliver A.
function umerge($arrays){
$result = array();
foreach($arrays as $array){
$array = (array) $array;
foreach($array as $value){
if(array_search($value,$result)===false)$result[]=$value;
}
}
return $result;
}
回答by danorton
To answer the question as asked, for a general solution that also works with associative arrays while preserving keys, I believe that you will find this solution most satisfactory:
为了回答所问的问题,对于在保留键的同时也适用于关联数组的通用解决方案,我相信您会发现这个解决方案最令人满意:
/**
* array_merge_unique - return an array of unique values,
* composed of merging one or more argument array(s).
*
* As with array_merge, later keys overwrite earlier keys.
* Unlike array_merge, however, this rule applies equally to
* numeric keys, but does not necessarily preserve the original
* numeric keys.
*/
function array_merge_unique(array $array1 /* [, array $...] */) {
$result = array_flip(array_flip($array1));
foreach (array_slice(func_get_args(),1) as $arg) {
$result =
array_flip(
array_flip(
array_merge($result,$arg)));
}
return $result;
}
回答by Stephen
array_merge will ignore numeric keys, so in your example array_merge($a, $b)
would give you $ab
, there is no need to call array_unique()
.
array_merge 将忽略数字键,所以在你的例子中array_merge($a, $b)
会给你$ab
,没有必要调用array_unique()
.
if you have string keys (i.e. an associative array) use array_values()
first:
如果您有字符串键(即关联数组),array_values()
请先使用:
array_merge(array_values($a), array_values($b));
回答by woot586
$a = array(0 => 0, 1 => 1, 2 => 2);
$b = array(0 => 2, 1 => 3, 2 => 4);
//add any from b to a that do not exist in a
foreach($b as $item){
if(!in_array($item,$b)){
$a[] = $item
}
}
//sort the array
sort($a);