Php 如何在多维关联数组中检测重复值?
我有一个关联多维数组:Php 如何在多维关联数组中检测重复值?,php,arrays,multidimensional-array,array-unique,Php,Arrays,Multidimensional Array,Array Unique,我有一个关联多维数组: Array ( [0] => Array ( [customer_name] => John Dow [customer_email] => john@example.com [customer_mobile] => 1236547895 [birth_date] => 12/1/1996 [stat
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\u mobile
和customer\u email
中的相似值,以减少冗余。联系人号码和电子邮件地址必须是非冗余的
所以请引导我,我如何才能做到这一点?谢谢:)您可以这样做(我从head生成代码,这样它可能会有bug-但想法应该很清楚)(我假设您的数组名为$persons):
因此,每个使用冗余手机或电子邮件的人都将字段
redundant\u email
或redundant\u mobile
设置为true。变量$discard=true
表示数组是冗余的 因为你不需要知道哪个,而只需要知道如果,你可以使用+:()
如果您需要匹配电子邮件和手机,请在相同的
中进行匹配,如果:
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);
这只是一个例子。尝试使用foreach
。您只需遍历数组一次,将email和mobile用作唯一键,具有相同唯一键的元素将只保留最后一个。如果希望结果使用数字索引,请使用$result
上的array\u values()
$result = [];
foreach($array as $v)
{
$result[$v['customer_email'] . $v['customer_mobile']] = $v;
}
我的答案是,您根本不应该在PHP中这样做。在本例中,应仅在数据库端检查/验证/过滤数据。如果存在重复项,则根本不必获取数据
运行查询以仅检查数据库中的冗余。只有在没有冗余的情况下才能获取数据
如果有大量数据,那么您将从一开始就进行大数据获取和循环数据
祝你好运。这是我的解决方案,效果很好
$name = array_column($array, 'name');
$filteredKeys = array_unique($name);
foreach (array_keys($filteredKeys) as $key => $value) {
$filtered [] = $array[$value];
}
return $filtered;
}
发布所需结果我不想要任何结果。我只想检查一下是否有客户有重复的联系电话和电子邮件地址。返回为标志-true表示此数组包含冗余Y您将其写入返回为标志-所有项目的一个标志或每个项目单独的一个标志?所有项目的一个标志-如果1个联系人号码重复2次或多次以及电子邮件。整个数组将被丢弃数组中没有任何值。。!如果1个联系电话和电子邮件重复2次或以上。整个阵列将被丢弃。。我如何才能做到这一点?@DevendraSingh-我再次根据您的新要求进行更正-现在您在$discard
变量中有布尔答案,在冗余人员中有“flags”
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"
}
}
$result = [];
foreach($array as $v)
{
$result[$v['customer_email'] . $v['customer_mobile']] = $v;
}
$name = array_column($array, 'name');
$filteredKeys = array_unique($name);
foreach (array_keys($filteredKeys) as $key => $value) {
$filtered [] = $array[$value];
}
return $filtered;
}