从三个字段中筛选唯一的数组值


Filter unique array values from three fields

如何过滤的唯一数组值

  • 主要联系人电子邮件
  • 首选电话
  • 名字

我正在使用

array_unique($data, SORT_REGULAR);

但是它检查所有的值。如何筛选三个字段?样本数据:

Array
(
    [0] => Array
        (
            [First Name] => xyz
            [Last Name] => abc
            [Primary Contact Email] => xyz@hotmail.com
            [Preferred Telephone] => 1-123-000-123
        )
    [1] => Array
        (
            [First Name] => xyz
            [Last Name] => abc
            [Primary Contact Email] => xyz@hotmail.com
            [Preferred Telephone] => 1-123-000-123
        )
    [2] => Array
        (
            [First Name] => dss
            [Last Name] => sddfs
            [Primary Contact Email] => dss@hotmail.com
            [Preferred Telephone] => 1-553-000-123
        )
    [3] => Array
        (
            [First Name] => dss
            [Last Name] => sds
            [Primary Contact Email] => dss@hotmail.com
            [Preferred Telephone] => 1-444-000-123
        )
    [4] => Array
        (
            [First Name] => dss
            [Last Name] => sds
            [Primary Contact Email] => dss@hotmail.com
            [Preferred Telephone] => 1-553-000-123
        )
)

目标数据:

Array
(
    [0] => Array
        (
            [First Name] => xyz
            [Last Name] => abc
            [Primary Contact Email] => xyz@hotmail.com
            [Preferred Telephone] => 1-123-000-123
        )
    [2] => Array
        (
            [First Name] => dss
            [Last Name] => sddfs
            [Primary Contact Email] => dss@hotmail.com
            [Preferred Telephone] => 1-553-000-123
        )
    [3] => Array
        (
            [First Name] => dss
            [Last Name] => sds
            [Primary Contact Email] => dss@hotmail.com
            [Preferred Telephone] => 1-444-000-123
        )
)

这是我的解决方案。输入数组称为$inputs。为了让它与您的代码一起工作,您需要更改数组键。在这里,我使用firstmailphone来进行简要说明。您的密钥是First NamePrimary Contact EmailPreferred Telephone

$inputs = [...];// your starting data
$results = []; // Starts empty; will be filled with the output
foreach($inputs as $input){
    $isDuplicate = false;
    foreach($results as $result){
        if(
            strtolower($input['first'])===strtolower($result['first']) &&
            strtolower($input['mail'])===strtolower($result['mail']) &&
            $input['phone']===$result['phone']
        ){
            //a duplicate was found in results
            $isDuplicate = true;
            break;
        }
    }
    //if no duplicate was found, add this input to results.
    if(!$isDuplicate) $results[]=$input;
}

实时演示

您可以在array_filter中使用匿名函数来过滤出相同的数据。方法如下:

        <?php
            $arrTmp         = array();
            $arrContactData = array(
                array(
                    "First Name"            => "xyz",
                    "Last Name"             => "abc",
                    "Primary Contact Email" => "xyz@hotmail.com",
                    "Preferred Telephone"   => "1-123-000-123",
                ),
                array(
                    "First Name"            => "xyz",
                    "Last Name"             => "differentLastName",
                    "Primary Contact Email" => "xyz@hotmail.com",
                    "Preferred Telephone"   => "1-123-000-123",
                ),
                array(
                    "First Name"            => "xyz",
                    "Last Name"             => "abc",
                    "Primary Contact Email" => "xyz@hotmail.com",
                    "Preferred Telephone"   => "1-123-000-123",
                ),
                array(
                    "First Name"            => "dss",
                    "Last Name"             => "sds",
                    "Primary Contact Email" => "dss@hotmail.com",
                    "Preferred Telephone"   => "1-444-000-123",
                ),
                array(
                    "First Name"            => "dss",
                    "Last Name"             => "sds",
                    "Primary Contact Email" => "dss@hotmail.com",
                    "Preferred Telephone"   => "1-553-000-123",
                ),
                array(
                    "First Name"            => "dss",
                    "Last Name"             => "sds",
                    "Primary Contact Email" => "dss@hotmail.com",
                    "Preferred Telephone"   => "1-553-000-123",
                ),
            );
            $arrFiltered = array_filter($arrContactData, function ($data){
                global $arrTmp;
                if(is_array($data)) {
                    if (!in_array($data, $arrTmp)) {
                        foreach($arrTmp as $arr){
                            if( strtolower($arr['Last Name'])               != strtolower($data['Last Name']) &&
                                strtolower($arr['First Name'])              == strtolower($data['First Name']) &&
                                strtolower($arr['Primary Contact Email'])   == strtolower($data['Primary Contact Email']) &&
                                strtolower($arr['Preferred Telephone'])     == strtolower($data['Preferred Telephone'])){
                                return null;
                            }
                        }
                        $arrTmp[] = $data;
                        return $data;
                    }               
                }
                return null;
            });
            var_dump($arrFiltered);
            // PRODUCES::
            array (size=3)
              0 => 
                array (size=4)
                  'First Name' => string 'xyz' (length=3)
                  'Last Name' => string 'abc' (length=3)
                  'Primary Contact Email' => string 'xyz@hotmail.com' (length=15)
                  'Preferred Telephone' => string '1-123-000-123' (length=13)
              2 => 
                array (size=4)
                  'First Name' => string 'dss' (length=3)
                  'Last Name' => string 'sds' (length=3)
                  'Primary Contact Email' => string 'dss@hotmail.com' (length=15)
                  'Preferred Telephone' => string '1-444-000-123' (length=13)
              3 => 
                array (size=4)
                  'First Name' => string 'dss' (length=3)
                  'Last Name' => string 'sds' (length=3)
                  'Primary Contact Email' => string 'dss@hotmail.com' (length=15)
                  'Preferred Telephone' => string '1-553-000-123' (length=13)

在这里自己测试一下。