1

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] => [email protected]
        )

    [1] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )        

    [2] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => [email protected]
        )        

)

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

5
  • So you want to remove index 2 because of the matching fields in index 0? Commented Aug 15, 2017 at 18:10
  • @MinistryofChaps Yes, i want to check if all fields match BUT RecipientEmail Commented Aug 15, 2017 at 18:11
  • @Don'tPanic Yes, no matter what email is, if all other fields already exists, i want to delete the item from the array Commented Aug 15, 2017 at 18:13
  • Did you give up? Commented Aug 17, 2017 at 21:42
  • @AbraCadaver Actually i wrote a foreach statement do delete myself. Anyway, there is good options in the solution, i will accept the first one. Commented Aug 18, 2017 at 17:45

3 Answers 3

2

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);
            }
        );
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

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' => '[email protected]'
    ],
    [
        'RecipientID' => '2',
        'RecipientScreenname' => 'Tom hanks L',
        'RecipientFirstname' => 'Thomas',
        'RecipientEmail' => '[email protected]',
   ],
    [
        'RecipientID' => '3',
        'RecipientScreenname' => 'Tom L',
        'RecipientFirstname' => 'Thomas',
        'RecipientEmail' => '[email protected]',
   ],
    [
        'RecipientID' => '4',
        'RecipientScreenname' => '444',
        'RecipientFirstname' => 'Thomas',
        'RecipientEmail' => '[email protected]',
   ],
    [
        'RecipientID' => '2',
        'RecipientScreenname' => 'Tom hanks L',
        'RecipientFirstname' => 'Thomas',
        'RecipientEmail' => '[email protected]',
   ],
   [
        'RecipientID' => '1',
        'RecipientScreenname' => 'Lau T',
        'RecipientFirstname' => 'TK',
        'RecipientEmail' => '[email protected]',
    ]  
];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.