繁体   English   中英

在不使用array_unique的情况下删除数组中的重复项

[英]Remove duplicates in array, without using array_unique

我需要删除阵列中的重复项,但它们并非完全相同(因此我不能使用array_unique)。 实际上,我需要忽略“重复检查”中的一个字段。 在下面的示例中,我希望在检查中忽略“ 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
        )        

)

有什么办法可以使用任何本机PHP函数吗?

没有循环的一线解决方案:

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

相同但格式:

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

只需在RecipientID重新索引,您将只有一个。 如果需要第一个使用此方法,如果需要最后一个,请使用array_reverse($array)

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

如果RecipientID不适用于唯一性,则可以使用值构建密钥。 再次使用this或array_reverse($array)

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

然后,如果需要, $result = array_values($result)

我为您编写了一个函数。 查看是否可行:

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'));

尝试这样的输入,例如:

$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.

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