我有一个数组如下
array(
0 => array('email' => 'abc@abc.com','name'=>'abc'),
1 => array('email' => 'xyz@abc.com','name'=>'xyz'),
2 => array('email' => 'uvw@abc.com','name'=>'uvw'),
3 => array('email' => 'abc@abc.com','name'=>'str'),
)
我想过滤掉电子邮件地址上的记录,并获取具有相同电子邮件地址的记录。例如,从上面的例子我想要
array(
0 => array(
array(
0 => array('email' => 'abc@abc.com','name'=>'abc'),
1 => array('email' => 'abc@abc.com','name'=>'str'),
)
)
我的代码是
$tmpArray = array();
$duplicateRecords = array();
if (empty($data)) {
return false;
}
foreach ($data as $key => $value) {
if (in_array($value['Email'], $tmpArray)) {
$duplicateRecords[] = $value;
}
$tmpArray[] = $value['Email'];
}
echo '<pre>';print_r($duplicateRecords);die;
但是这段代码只返回记录一次存在,这是第二次。我知道什么时候它第一次遍历它没有电子邮件进行比较。有没有办法让记录的存在次数和数组一样多。
答案 0 :(得分:1)
// get count of each email
$counters = array_count_values(array_column($data, 'email'));
// collect email with counter > 1
$result = [];
foreach ($data as $item) {
if ($counters[$item['email']] > 1) {
$result[] = $item;
}
}
答案 1 :(得分:1)
这个应该适合你:
foreach($array as $key => $value) {
foreach ($array as $k => $v) {
if ($key < $k && $value['email'] == $v['email']) {
$result[] = array(
$value,
$v
);
}
}
}
PHPFiddle链接:http://phpfiddle.org/main/code/8trq-k2zc
请注意:这只会找到冲突对。例如:
$array = array(
array(
'email' => 'abc@abc.com',
'name' => 'abc'
),
array(
'email' => 'abc@abc.com',
'name' => 'def'
),
array(
'email' => 'abc@abc.com',
'name' => 'ghi'
)
);
会导致:
Array
(
[0] => Array
(
[0] => Array
(
[email] => abc@abc.com
[name] => abc
)
[1] => Array
(
[email] => abc@abc.com
[name] => def
)
)
[1] => Array
(
[0] => Array
(
[email] => abc@abc.com
[name] => abc
)
[1] => Array
(
[email] => abc@abc.com
[name] => ghi
)
)
[2] => Array
(
[0] => Array
(
[email] => abc@abc.com
[name] => def
)
[1] => Array
(
[email] => abc@abc.com
[name] => ghi
)
)
)
所以abc
与def
冲突,abc
与ghi
冲突,def
与ghi
冲突。
答案 2 :(得分:1)
这是一个两通#&#39;解。代码已注释。
PHP 5.3.18
<?php // https://stackoverflow.com/questions/26548634/find-all-duplicates-values-in-multi-dimensional-array-php
$data = array(
0 => array('email' => 'abc@abc.com','name'=>'abc'),
1 => array('email' => 'xyz@abc.com','name'=>'xyz'),
2 => array('email' => 'uvw@abc.com','name'=>'uvw'),
3 => array('email' => 'abc@abc.com','name'=>'str'),
);
// two passes required
// first pass: count of emails
$emailCounts = array();
foreach($data as $emailEntry) {
if (isset($emailCounts[$emailEntry['email']])) {
$emailCounts[$emailEntry['email']] += 1;
}
else {
$emailCounts[$emailEntry['email']] = 1;
}
}
// second pass: extract duplicate emails (count > 1)
$duplicateEmails = array();
foreach($data as $emailEntry) {
if ($emailCounts[$emailEntry['email']] > 1) {
$duplicateEmails[] = $emailEntry;
}
}
// show output...
var_dump($emailCounts);
var_dump($duplicateEmails);
实际输出:
array
'abc@abc.com' => int 2
'xyz@abc.com' => int 1
'uvw@abc.com' => int 1
array
0 =>
array
'email' => string 'abc@abc.com' (length=11)
'name' => string 'abc' (length=3)
1 =>
array
'email' => string 'abc@abc.com' (length=11)
'name' => string 'str' (length=3)
答案 3 :(得分:0)
Here is the code for you
>$mainarray=array(
0 => array('email' => 'abc@abc.com','name'=>'abc'),
1 => array('email' => 'xyz@abc.com','name'=>'xyz'),
2 => array('email' => 'uvw@abc.com','name'=>'uvw'),
3 => array('email' => 'abc@abc.com','name'=>'str'),
);
foreach($mainarray as $single){
$emailarray[]=$single['email'];
}
foreach(array_count_values($emailarray) as $val => $c)
if($c > 1) $dups[] = $val;
foreach($mainarray as $single){
if(in_array($single['email'], $dups)){
$result[]=$single;
}
}
echo '<pre>';print_r($result);