[英]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). 我需要删除阵列中的重复项,但它们并非完全相同(因此我不能使用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 :
在下面的示例中,我希望在检查中忽略“ RecipientEmail”,因此应该删除第三个元素:
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 ? 有什么办法可以使用任何本机PHP函数吗?
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. 只需在
RecipientID
重新索引,您将只有一个。 If you need the first use this and if you need the last then use array_reverse($array)
: 如果需要第一个使用此方法,如果需要最后一个,请使用
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. 如果
RecipientID
不适用于唯一性,则可以使用值构建密钥。 Again, use this or array_reverse($array)
: 再次使用this或
array_reverse($array)
:
foreach($array as $v) {
$result[$v['RecipientID'].'-'
.$v['RecipientScreenname'].'-'
.$v['RecipientFirstname']
] = $v;
}
Then $result = array_values($result)
if needed. 然后,如果需要,
$result = array_values($result)
。
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',
]
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.