我有一个关联的多维数组:
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
中相似的值,以减少冗余。联系电话和电子邮件地址必须是非冗余的。
所以请指导我,我怎样才能做到这一点?谢谢:))
答案 0 :(得分:0)
你可以这样做(我从头部生成代码,因此它可能有bug - 但想法应该清楚)(我假设你的数组名称是$ person):
$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
表示该数组是多余的。
答案 1 :(得分:0)
由于您不需要知道哪个,但只有 ,您可以使用array_column + array_unique :( { {3}})
$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';
}
答案 2 :(得分:0)
简单的解决方案是:
<?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"
}
}
这只是一个例子。
答案 3 :(得分:0)
使用sum(examMarks, [])
尝试此操作。您只需要遍历数组一次,使用电子邮件和移动设备作为唯一键,具有相同唯一键的元素将只保留最后一个。如果您希望结果使用数字索引,请使用foreach
上的array_values()
。
$result
答案 4 :(得分:0)
我的回答是,你根本不应该在PHP中这样做。在您提供的案例中,应仅在数据库端检查/验证/过滤数据。如果有重复项,那么您根本不需要获取数据!
运行查询以检查db中的冗余。只有没有冗余才能获取数据。
如果有大量数据,那么您将节省大量数据并从头开始循环数据。
祝你好运。答案 5 :(得分:0)
这是我的解决方案,并且工作正常。
$name = array_column($array, 'name');
$filteredKeys = array_unique($name);
foreach (array_keys($filteredKeys) as $key => $value) {
$filtered [] = $array[$value];
}
return $filtered;
}