如何检测多维关联数组中的重复值?
How to detect duplicate values in multidimensional associative array?
我有一个多维关联数组:
Array
(
[0] => Array
(
[customer_name] => John Dow
[customer_email] => john@example.com
[customer_mobile] => 1236547895
[birth_date] => 12/1/1996
[status] => Enable
)
[1] => Array
(
[customer_name] => Alex
[customer_email] => alex@example.com
[customer_mobile] => 4563214785
[birth_date] => 19/1/1996
[status] => Enable
)
[2] => Array
(
[customer_name] => Arina
[customer_email] => arina@example.com
[customer_mobile] => 963214785
[birth_date] => 25/1/1996
[status] => Enable
)
[3] => Array
(
[customer_name] => Atom
[customer_email] => atom@example.com
[customer_mobile] => 5214789632
[birth_date] => 12/1/1998
[status] => Enable
)
[4] => Array
(
[customer_name] => Jennifer
[customer_email] => jennifer@example.com
[customer_mobile] => 4563214785
[birth_date] => 12/2/1996
[status] => Enable
)
)
现在我想检查 customer_mobile
和 customer_email
中彼此相似的值以减少冗余。联系电话和电子邮件地址不能重复。
所以请指导我,我怎样才能做到这一点?谢谢:)
你可以这样做(我从头生成代码所以它可能有错误 - 但想法应该清楚)(我假设你的数组名称是 $persons):
$emails = [];
$mobiles = [];
$discard = false;
foreach($persons as $person)
{
$email = $person['customer_email'];
if(!isset($emails[$email])) {
$emails[$email] = $person;
} else {
$emails[$email]['redundant_email']=true;
$person['redundant_email']=true;
$discard = true;
}
$mobile = $person['customer_mobile'];
if(!isset($mobiles[$mobile])) {
$mobiles[$mobile] = $person;
} else {
$mobiles[$mobile]['redundant_mobile']=true;
$person['redundant_mobile']=true;
$discard = true;
}
}
因此,每个拥有冗余手机或电子邮件的人都将字段 redundant_email
或 redundant_mobile
设置为 true。变量 $discard=true
表示数组是多余的。
因为你不需要知道哪个,而只需要知道if,你可以使用array_column + array_unique: (run)
$cm = array_column($arr, 'customer_mobile');
if($cm != array_unique($cm)){
echo 'There are duplicates in customer_mobile';
}
$ce = array_column($arr, 'customer_email');
if($cm != array_unique($ce)){
echo 'There are duplicates in customer_email';
}
如果您需要同时匹配电子邮件和手机,请在同一个if
:
中进行
if($cm != array_unique($cm) && $ce != array_unique($ce)){
echo 'There are duplicates in both customer_mobile and customer_email';
}
简单的解决方案是:
<?php
$data = [
[
'name' => 'name 1',
'phone' => '12341234',
'email' => 'test@web.com'
],
[
'name' => 'name 2',
'phone' => '12341234',
'email' => 'test@web1.com'
],
[
'name' => 'name 3',
'phone' => '4322342',
'email' => 'test@web1.com'
],
[
'name' => 'name 4',
'phone' => '1234123423',
'email' => 'test@web1.com'
],
[
'name' => 'name 5',
'phone' => '12341266634',
'email' => 'test@eqweqwweb.com'
],
];
$phones = [];
$emails = [];
foreach ($data as $key => $contact) {
if (array_search($contact['phone'], $phones) !== false || array_search($contact['email'], $emails) !== false) {
unset($data[$key]);
} else {
$phones[] = $contact['phone'];
$emails[] = $contact['email'];
}
}
var_dump($data);
结果你会得到:
array(3) {
[0] =>
array(3) {
'name' =>
string(6) "name 1"
'phone' =>
string(8) "12341234"
'email' =>
string(12) "test@web.com"
}
[2] =>
array(3) {
'name' =>
string(6) "name 3"
'phone' =>
string(7) "4322342"
'email' =>
string(13) "test@web1.com"
}
[4] =>
array(3) {
'name' =>
string(6) "name 5"
'phone' =>
string(11) "12341266634"
'email' =>
string(18) "test@eqweqwweb.com"
}
}
这只是示例。
用 foreach
试试这个。只需要遍历数组一次,使用email和mobile作为唯一键,唯一键相同的元素只保留最后一个。如果您希望结果使用数字索引,请在 $result
.
上使用 array_values()
$result = [];
foreach($array as $v)
{
$result[$v['customer_email'] . $v['customer_mobile']] = $v;
}
我的回答是,你根本不应该在 PHP 中这样做。在您提出的案例中,数据应该 checked/validated/filtered 仅在数据库端。如果有重复,那你根本不用去取数据!
运行 仅检查数据库中冗余的查询。只有在没有冗余的情况下才获取数据。
如果有很多数据,那么您将节省大量数据获取并从头开始循环数据。
祝你好运。
这是我的解决方案,运行良好。
$name = array_column($array, 'name');
$filteredKeys = array_unique($name);
foreach (array_keys($filteredKeys) as $key => $value) {
$filtered [] = $array[$value];
}
return $filtered;
}
我有一个多维关联数组:
Array
(
[0] => Array
(
[customer_name] => John Dow
[customer_email] => john@example.com
[customer_mobile] => 1236547895
[birth_date] => 12/1/1996
[status] => Enable
)
[1] => Array
(
[customer_name] => Alex
[customer_email] => alex@example.com
[customer_mobile] => 4563214785
[birth_date] => 19/1/1996
[status] => Enable
)
[2] => Array
(
[customer_name] => Arina
[customer_email] => arina@example.com
[customer_mobile] => 963214785
[birth_date] => 25/1/1996
[status] => Enable
)
[3] => Array
(
[customer_name] => Atom
[customer_email] => atom@example.com
[customer_mobile] => 5214789632
[birth_date] => 12/1/1998
[status] => Enable
)
[4] => Array
(
[customer_name] => Jennifer
[customer_email] => jennifer@example.com
[customer_mobile] => 4563214785
[birth_date] => 12/2/1996
[status] => Enable
)
)
现在我想检查 customer_mobile
和 customer_email
中彼此相似的值以减少冗余。联系电话和电子邮件地址不能重复。
所以请指导我,我怎样才能做到这一点?谢谢:)
你可以这样做(我从头生成代码所以它可能有错误 - 但想法应该清楚)(我假设你的数组名称是 $persons):
$emails = [];
$mobiles = [];
$discard = false;
foreach($persons as $person)
{
$email = $person['customer_email'];
if(!isset($emails[$email])) {
$emails[$email] = $person;
} else {
$emails[$email]['redundant_email']=true;
$person['redundant_email']=true;
$discard = true;
}
$mobile = $person['customer_mobile'];
if(!isset($mobiles[$mobile])) {
$mobiles[$mobile] = $person;
} else {
$mobiles[$mobile]['redundant_mobile']=true;
$person['redundant_mobile']=true;
$discard = true;
}
}
因此,每个拥有冗余手机或电子邮件的人都将字段 redundant_email
或 redundant_mobile
设置为 true。变量 $discard=true
表示数组是多余的。
因为你不需要知道哪个,而只需要知道if,你可以使用array_column + array_unique: (run)
$cm = array_column($arr, 'customer_mobile');
if($cm != array_unique($cm)){
echo 'There are duplicates in customer_mobile';
}
$ce = array_column($arr, 'customer_email');
if($cm != array_unique($ce)){
echo 'There are duplicates in customer_email';
}
如果您需要同时匹配电子邮件和手机,请在同一个if
:
if($cm != array_unique($cm) && $ce != array_unique($ce)){
echo 'There are duplicates in both customer_mobile and customer_email';
}
简单的解决方案是:
<?php
$data = [
[
'name' => 'name 1',
'phone' => '12341234',
'email' => 'test@web.com'
],
[
'name' => 'name 2',
'phone' => '12341234',
'email' => 'test@web1.com'
],
[
'name' => 'name 3',
'phone' => '4322342',
'email' => 'test@web1.com'
],
[
'name' => 'name 4',
'phone' => '1234123423',
'email' => 'test@web1.com'
],
[
'name' => 'name 5',
'phone' => '12341266634',
'email' => 'test@eqweqwweb.com'
],
];
$phones = [];
$emails = [];
foreach ($data as $key => $contact) {
if (array_search($contact['phone'], $phones) !== false || array_search($contact['email'], $emails) !== false) {
unset($data[$key]);
} else {
$phones[] = $contact['phone'];
$emails[] = $contact['email'];
}
}
var_dump($data);
结果你会得到:
array(3) {
[0] =>
array(3) {
'name' =>
string(6) "name 1"
'phone' =>
string(8) "12341234"
'email' =>
string(12) "test@web.com"
}
[2] =>
array(3) {
'name' =>
string(6) "name 3"
'phone' =>
string(7) "4322342"
'email' =>
string(13) "test@web1.com"
}
[4] =>
array(3) {
'name' =>
string(6) "name 5"
'phone' =>
string(11) "12341266634"
'email' =>
string(18) "test@eqweqwweb.com"
}
}
这只是示例。
用 foreach
试试这个。只需要遍历数组一次,使用email和mobile作为唯一键,唯一键相同的元素只保留最后一个。如果您希望结果使用数字索引,请在 $result
.
array_values()
$result = [];
foreach($array as $v)
{
$result[$v['customer_email'] . $v['customer_mobile']] = $v;
}
我的回答是,你根本不应该在 PHP 中这样做。在您提出的案例中,数据应该 checked/validated/filtered 仅在数据库端。如果有重复,那你根本不用去取数据!
运行 仅检查数据库中冗余的查询。只有在没有冗余的情况下才获取数据。
如果有很多数据,那么您将节省大量数据获取并从头开始循环数据。
祝你好运。
这是我的解决方案,运行良好。
$name = array_column($array, 'name');
$filteredKeys = array_unique($name);
foreach (array_keys($filteredKeys) as $key => $value) {
$filtered [] = $array[$value];
}
return $filtered;
}