0

I have a array as following

array(
  0 => array('email' => '[email protected]','name'=>'abc'),
  1 => array('email' => '[email protected]','name'=>'xyz'),
  2 => array('email' => '[email protected]','name'=>'uvw'),
  3 => array('email' => '[email protected]','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' => '[email protected]','name'=>'abc'),
         1 => array('email' => '[email protected]','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.

2
  • So you want to group the elements of your array, themselves arrays, by the value of their "email" key. Commented Oct 24, 2014 at 13:30
  • @LightnessRacesinOrbit Yes I want to group array elements having same email id Commented Oct 24, 2014 at 13:34

4 Answers 4

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

array_column available since php5.5
@Vleonov, Hi I have updated my question regards resultant array format. If I want resultant array like array( 0 => array( array( 0 => array('email' => '[email protected]','name'=>'abc'), 1 => array('email' => '[email protected]','name'=>'str'), ) ) to have multiple values?
@AwaisQarni, then do this: $result[$item['email'][] = $item; It will group array by email, as you wish
1

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' => '[email protected]',
        'name' => 'abc'
    ),
    array(
        'email' => '[email protected]',
        'name' => 'def'
    ),
    array(
        'email' => '[email protected]',
        'name' => 'ghi'
    )
);

Would result in:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [email] => [email protected]
                    [name] => abc
                )

            [1] => Array
                (
                    [email] => [email protected]
                    [name] => def
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [email] => [email protected]
                    [name] => abc
                )

            [1] => Array
                (
                    [email] => [email protected]
                    [name] => ghi
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [email] => [email protected]
                    [name] => def
                )

            [1] => Array
                (
                    [email] => [email protected]
                    [name] => ghi
                )

        )

)

So abc conflicts with def, abc conflicts with ghi, and def conflicts with ghi.

Comments

1

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' => '[email protected]','name'=>'abc'),
  1 => array('email' => '[email protected]','name'=>'xyz'),
  2 => array('email' => '[email protected]','name'=>'uvw'),
  3 => array('email' => '[email protected]','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
  '[email protected]' => int 2
  '[email protected]' => int 1
  '[email protected]' => int 1

array
  0 => 
    array
      'email' => string '[email protected]' (length=11)
      'name' => string 'abc' (length=3)
  1 => 
    array
      'email' => string '[email protected]' (length=11)
      'name' => string 'str' (length=3)

Comments

0
Here is the code for you
>$mainarray=array(
  0 => array('email' => '[email protected]','name'=>'abc'),
 1 => array('email' => '[email protected]','name'=>'xyz'),
 2 => array('email' => '[email protected]','name'=>'uvw'),
 3 => array('email' => '[email protected]','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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.