Reputation: 7428
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 ?
Upvotes: 1
Views: 1576
Reputation: 2871
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);
}
);
Upvotes: 2
Reputation: 1354
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]',
]
];
Upvotes: 1
Reputation: 78984
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.
Upvotes: 1