充分利用2个数组唯一值 [英] Getting unique values from 2 arrays

查看:145
本文介绍了充分利用2个数组唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个阵列,我试图只从他们那里得到的唯一值。所以,我不只是想删除重复的,我其实是想删除这两个副本。

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:

$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?

推荐答案

其他的答案是正确的轨道上,但是的 和array_diff 只在一个方向工作 - 即它返回存在给定的第一阵列中不在任何其他的值。

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.

您想要做的就是让不同的两个方向,然后区别合并到一起:

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);

这篇关于充分利用2个数组唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆