php multi-dimensional array remove duplicate

2019-01-01 15:55发布

Not sure if this question is a duplicate in need of removal, but I couldn't find the answer elsewhere so I'll have a go at asking.

I've got a 2d array that looks as follows:

Array
(
[0] => Array
    (
        [0] => dave
        [1] => jones
        [2] => c@b.c
    )

[1] => Array
    (
        [0] => john
        [1] => jones
        [2] => a@b.c

    )

[2] => Array
    (
        [0] => bruce
        [1] => finkle
        [2] => c@b.c
    )
)

I'd like to remove those with duplicate emails. So in the above example I'd like to just remove either [][0] or [][2]. I'm not worried about checking against names or anything like that, I just need the sub arrays to be deduplicated based on a single value.

At the moment I have something like this

  if(is_array($array) && count($array)>0){
  foreach ($array as $subarray) {
    $duplicateEmail[$subarray[2]] = isset($duplicateEmail[$subarray[2]]);
    unset($duplicateEmail[$subarray[2]]);
   }
  }

but it just ain't right. Any help appreciated.

7条回答
一个人的天荒地老
2楼-- · 2019-01-01 16:31
$array = array(
    array('dave','jones','c@b.c'),
    array('dave','jones','a@c.d'),
    array('dave','jones','c@b.c'),
    array('dave','jones','e@v.d'),
    array('dave','jones','a@c.d')   
);

$copy = $array; // create copy to delete dups from
$usedEmails = array(); // used emails

for( $i=0; $i<count($array); $i++ ) {

    if ( in_array( $array[$i][2], $usedEmails ) ) {
        unset($copy[$i]);
    }
    else {
        $usedEmails[] = $array[$i][2];
    }

}

print_r($copy);
查看更多
临风纵饮
3楼-- · 2019-01-01 16:33

User SORT_REGULAR as second parameter.

$uniqueArray = array_unique($array, SORT_REGULAR);
查看更多
公子世无双
4楼-- · 2019-01-01 16:39

The user comments for array_unique() have a few solutions to this. For example

    function multi_unique($array) {
        foreach ($array as $k=>$na)
            $new[$k] = serialize($na);
        $uniq = array_unique($new);
        foreach($uniq as $k=>$ser)
            $new1[$k] = unserialize($ser);
        return ($new1);
    }

from http://uk.php.net/manual/en/function.array-unique.php#57202.

查看更多
裙下三千臣
5楼-- · 2019-01-01 16:40

A quick solution which uses the uniqueness of array indexes:

$newArr = array();
foreach ($array as $val) {
    $newArr[$val[2]] = $val;    
}
$array = array_values($newArr);

Notice 1: As visible from above, the last match for an email address is used instead of the first. This can be changed by replacing the second line with

foreach (array_reverse($array) as $val) {

Notice 2: The indexes in the resulting array are somewhat mixed up. But I guess this doesn't matter...

查看更多
浪荡孟婆
6楼-- · 2019-01-01 16:43

Much Simpler Solution.

$unique = array_map('unserialize', array_unique(array_map('serialize', $array)));

echo "<pre>";
print_r($unique);
查看更多
还给你的自由
7楼-- · 2019-01-01 16:49

You can use array_column()'s clever key-assignment feature by using null as the second parameter. This assigns new keys to each subarray while leaving the subarray data fully intact. These temporary keys ( element [2] aka "email values") will effectively cause new subarrays with duplicate emails to overwrite previous ones. Once the duplicates are purged, just re-index the array (if necessary) with array_values().

Code: (Demo)

$array = [
    ['dave', 'jones', 'c@b.c'],
    ['john', 'jones', 'a@b.c'],
    ['bruce', 'finkle', 'c@b.c']
];

var_export(array_values(array_column($array, null, 2)));

Output:

array (
  0 => 
  array (
    0 => 'bruce',
    1 => 'finkle',
    2 => 'c@b.c',
  ),
  1 => 
  array (
    0 => 'john',
    1 => 'jones',
    2 => 'a@b.c',
  ),
)
查看更多
登录 后发表回答