php 从 2 个数组中获取唯一值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3507419/
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
Getting unique values from 2 arrays
提问by lok
I have 2 arrays that I'm trying to get the unique values only from them. So I'm not just trying to remove duplicates, I'm actually trying to remove both duplicates.
我有 2 个数组,我试图仅从它们获取唯一值。所以我不只是想删除重复项,我实际上是在尝试删除两个重复项。
So if I'm getting the 2 arrays like this:
所以如果我得到这样的 2 个数组:
$array1 = array();
$array2 = array();
foreach($values1 as $value1){ //output: $array1 = 10, 15, 20, 25;
$array1[] = $value1;
}
foreach($values2 as $value2){ //output: $array2 = 10, 15, 100, 150;
$array2[] = $value2;
}
The final output I'm looking for is
我正在寻找的最终输出是
$output = 20, 25, 100, 150;
Any neat way to getting this done?
有什么巧妙的方法来完成这项工作吗?
回答by Daniel Vandersluis
The other answers are on the right track, but array_diff
only works in one direction -- ie. it returns the values that exist in the first array given that aren't in any others.
其他答案在正确的轨道上,但array_diff
只适用于一个方向——即。它返回存在于第一个数组中的值,而这些值不在任何其他数组中。
What you want to do is get the difference in both directions and then merge the differences together:
您要做的是获取两个方向的差异,然后将差异合并在一起:
$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);
$output = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
// $output will be (20, 25, 100, 150);
回答by Mattias Andersson
Not to detract from Daniel Vandersluis's answer, but to add to it...
不是要贬低 Daniel Vandersluis 的回答,而是要补充...
What you're looking for is basically an XOR operation of the arrays. To that end, "merlinyoda at dorproject dot net" provided the following routine, in a comment on http://php.net/manual/en/function.array-diff.php:
您正在寻找的基本上是数组的异或运算。为此,“dorproject dot net 的 merlinyoda”在http://php.net/manual/en/function.array-diff.php的评论中提供了以下例程:
<?php
function array_xor ($array_a, $array_b) {
$union_array = array_merge($array_a, $array_b);
$intersect_array = array_intersect($array_a, $array_b);
return array_diff($union_array, $intersect_array)
}
?>
This function takes a different approach to calculating the XOR.
此函数采用不同的方法来计算 XOR。
回答by Rohit Joshi
Try following code it works fine for numbers,String and every condition
尝试以下代码,它适用于数字、字符串和每个条件
The following Logic will only works for numbers.
以下逻辑仅适用于数字。
$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);
$output = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
The following logic will work fine for any condition
以下逻辑适用于任何条件
$a1 = array('[email protected]');
$a2 = array('[email protected]','[email protected]');
$new_array=array_merge($a1,$a2);
$unique=array_unique($new_array);
Thanks
谢谢
回答by Chuck Burgess
Here is the code to do it. It may be able to be optimized, but you get the idea:
这是执行此操作的代码。它可能能够被优化,但你明白了:
$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);
$new_array = array();
foreach($array1 as $value) {
if(!in_array($value, $array2)) {
array_push($new_array, $value);
}
}
foreach($array2 as $value) {
if(!in_array($value, $array1)) {
array_push($new_array, $value);
}
}
print_r($new_array);
To use array_diff, you would have to do:
要使用 array_diff,您必须执行以下操作:
$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);
$out1 = array_diff($array1, $array2);
$out2 = array_diff($array2, $array1);
$output = array_merge($out1, $out2);
print_r($output);
回答by Saverio Molinaro
Another good solution is this:
另一个很好的解决方案是:
$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);
$output = array_diff(array_merge($array1, $array2), array_intersect($array1, $array2));
// $output will be (20, 25, 100, 150);
$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);
$output = array_diff(array_merge($array1, $array2), array_intersect($array1, $array2));
// $output 将为 (20, 25, 100, 150);
回答by Frxstrem
The array_diff()
(manual) function can be used to find the difference between two arrays:
的array_diff()
(手动)函数可以被用于寻找两个阵列之间的区别是:
$array1 = array(10, 20, 40, 80);
$array2 = array(10, 20, 100, 200);
$diff = array_diff($array1, $array2);
// $diff = array(40, 80, 100, 200);
You can pass as many arrays as you want to the function, it is not just limited to two arrays.
您可以将任意数量的数组传递给函数,它不仅限于两个数组。