I have a array as following
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'),
)
I want to filter out records on email address and get records having same email address. For example from above example I want
array(
0 => array(
array(
0 => array('email' => 'abc@abc.com','name'=>'abc'),
1 => array('email' => 'abc@abc.com','name'=>'str'),
)
)
My code is
$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;
But this piece of code only returns the record's once existance, which is of second time. I know when It is traversing first time it isn't having email to compare. Is there any way to get existence of record as many times as it is in array.
// 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;
}
}
This one should work for you:
foreach($array as $key => $value) {
foreach ($array as $k => $v) {
if ($key < $k && $value['email'] == $v['email']) {
$result[] = array(
$value,
$v
);
}
}
}
PHPFiddle Link: http://phpfiddle.org/main/code/8trq-k2zc
Please note: this would only find you the conflicting pairs. For example:
$array = array(
array(
'email' => 'abc@abc.com',
'name' => 'abc'
),
array(
'email' => 'abc@abc.com',
'name' => 'def'
),
array(
'email' => 'abc@abc.com',
'name' => 'ghi'
)
);
Would result in:
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
)
)
)
So abc
conflicts with def
, abc
conflicts with ghi
, and def
conflicts with ghi
.
This is a 'two pass' solution. The code is commented.
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);
Actual output:
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)
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.