基于列的值在数组删除重复 [英] Remove duplicate in array based on column value
本文介绍了基于列的值在数组删除重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些帮助与此有关。我有以下数组:
I need some help with this. I have the following array:
$result = array(
0 => array('a'=>1,'b'=>'data1'),
1 => array('a'=>2,'b'=>'data2'),
2 => array('a'=>1,'b'=>'data3'),
);
我想通过比较列的'A'的值,以去除重复行。预期的输出应该是:
I want to remove duplicate rows by comparing the values of column 'a'. The expected output should be:
array(
0 => array('a'=>1,'b'=>'data1'),
1 => array('a'=>2,'b'=>'data2'),
);
或者
array(
1 => array('a'=>2,'b'=>'data2'),
2 => array('a'=>1,'b'=>'data3'),
);
有没有一种简单的方法来做到这一点?
Is there a simple way to do this?
推荐答案
您可以创建领域的所有可能值的一小阵 A
使用的 array_map()
,从中抓住所有唯一值与的 array_unique()
,然后使用与原阵列相交它 array_intersect_key()
。
You can create a small array of all possible values of field a
using array_map()
, grab all unique values from it with array_unique()
and then intersect it with the original array using array_intersect_key()
.
$output = array_intersect_key(
$result,
array_unique(array_map(function($item) {
return $item['a'];
}, $result))
);
或者,由于5.5:
Or, since 5.5:
$output = array_intersect_key($result, array_unique(array_column($result, 'a')));
这篇关于基于列的值在数组删除重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文