简体   繁体   中英

Remove duplicates in array, without using array_unique

I need to remove duplicates in my array, but they aren't EXACTLY duplicates (so i can't use array_unique). Actually i need to ignore one field in the 'duplicate check'. In the example below, i want the 'RecipientEmail' to be ignored in the check, so the third element should me removed :

Array
(
    [0] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => lau@xx.co.uk
        )

    [1] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )        

    [2] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => other@xx.co.uk
        )        

)

Is there any way to do it using any native PHP function ?

One line solution w/o loops:

array_filter($list, function($el) use(&$unique) { return isset($unique[$key = "{$el['RecipientID']}-{$el['RecipientScreenname']}-{$el['RecipientFirstname']}"]) ? 0 : ($unique[$key] = 1); });

Same but formatted:

array_filter(
            $list,
            function ($el) use (&$unique) {
                return isset($unique[$key = "{$el['RecipientID']}-{$el['RecipientScreenname']}-{$el['RecipientFirstname']}"]) ? 0 : ($unique[$key] = 1);
            }
        );

Just reindex on RecipientID and you will only have one. If you need the first use this and if you need the last then use array_reverse($array) :

$result = array_column($array, null, 'RecipientID');

If RecipientID doesn't work for uniqueness, then you can build a key with the values. Again, use this or array_reverse($array) :

foreach($array as $v) {
    $result[$v['RecipientID'].'-'
           .$v['RecipientScreenname'].'-'
           .$v['RecipientFirstname']
           ] = $v;
}

Then $result = array_values($result) if needed.

I wrote a function for you. See if this works:

function make_unique($input, $ignore_column)
{ 
    $input = array_reverse($input);
    $orig = $input; 
    array_walk($input, function (&$v) use ($ignore_column) {
            unset($v[$ignore_column]);
        });

    foreach($orig as $index =>$val)
    {
        unset($input[$index]);
        unset($val[$ignore_column]);
        if(in_array($val, $input))
            unset($orig[$index]); 
    } 

    return(array_reverse($orig));
}
var_dump($input);
var_dump(make_unique($input, 'RecipientEmail'));

try this with input such as this:

$input =[
   [
        'RecipientID' => '1',
        'RecipientScreenname' => 'Lau T',
        'RecipientFirstname' => 'TK',
        'RecipientEmail' => 'lau@xx.co.uk'
    ],
    [
        'RecipientID' => '2',
        'RecipientScreenname' => 'Tom hanks L',
        'RecipientFirstname' => 'Thomas',
        'RecipientEmail' => 'info@xx.com',
   ],
    [
        'RecipientID' => '3',
        'RecipientScreenname' => 'Tom L',
        'RecipientFirstname' => 'Thomas',
        'RecipientEmail' => 'info@xx.com',
   ],
    [
        'RecipientID' => '4',
        'RecipientScreenname' => '444',
        'RecipientFirstname' => 'Thomas',
        'RecipientEmail' => 'info@xx.com',
   ],
    [
        'RecipientID' => '2',
        'RecipientScreenname' => 'Tom hanks L',
        'RecipientFirstname' => 'Thomas',
        'RecipientEmail' => 'infsdfsdfo@xx.com',
   ],
   [
        'RecipientID' => '1',
        'RecipientScreenname' => 'Lau T',
        'RecipientFirstname' => 'TK',
        'RecipientEmail' => 'other@xx.co.uk',
    ]  
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM