如何过滤
的唯一数组值我正在使用
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
)
)
答案 0 :(得分:0)
您可以在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)
自己测试here。
答案 1 :(得分:0)
这是我的解决方案。输入数组称为$inputs
。要使其与代码一起使用,您需要更改数组键。在这里,我使用first
,mail
和phone
作为简要介绍。您的密钥为First Name
,Primary Contact Email
和Preferred 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;
}