PHP删除关联数组的数组,这些数组具有特定键的重复值 [英] PHP remove array of associative array that have a duplicate values of specific key

查看:69
本文介绍了PHP删除关联数组的数组,这些数组具有特定键的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关联数组的数组

I've a array of associative array

array(xxx) {
 [0]=>
   array(3) {
    ["group_id"]=>2
    ["contact"]=> "foo"
    ["contact_email"]=> "foo@gmail.com"
   }
 [1]=>
   array(3) {
    ["group_id"]=>2
    ["contact"]=> "bar"
    ["contact_email"]=> "bar@gmail.com"
   }
  [2]=>
   array(3) {
    ["group_id"]=>2
    ["contact"]=> "foobar"
    ["contact_email"]=> "bar@gmail.com"
   }
 [3]=>
   array(3) {
    ["group_id"]=>2
    ["contact"]=> "bar"
    ["contact_email"]=> "bar@gmail.com"
   }

我要删除重复的数组

array_unique( $array, SORT_REGULAR );

但是现在我想做些更具体的事情,即只消除具有重复键值(contact_email)的数组以获得此结果

But now I would like to do something more specific by eliminating only the arrays that have duplicated key value (contact_email) to obtain this result

 array(xxx) {
 [0]=>
   array(3) {
    ["group_id"]=>2
    ["contact"]=> "foo"
    ["contact_email"]=> "foo@gmail.com"
   }
 [1]=>
   array(3) {
    ["group_id"]=>2
    ["contact"]=> "bar"
    ["contact_email"]=> "bar@gmail.com"
   }

我该怎么做?
谢谢

How could i do that?
Thank you

推荐答案

通过 contact_email 提取到数组并建立索引.由于不能有重复的索引,您将得到最后一次出现:

Extract to an array and index by contact_email. Since there cannot be duplicate indexes you'll get the last occurrence:

$array = array_column($array, null, 'contact_email');

如果您希望将其重新索引回整数:

If you want to re-index that back to integers:

$array = array_values(array_column($array, null, 'contact_email'));

这篇关于PHP删除关联数组的数组,这些数组具有特定键的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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