tags:

views:

219

answers:

4

i want to retrieve all duplicated entries from a array, how can it be possible in php.

array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'hello', 5=>'U');

i want to return an array having hello

out put array

array(1 =>'Hello' ,2=>'hello');
+1  A: 

Try:

$arr2 = array_diff_key($arr, array_unique($arr));


case insensitive:

array_diff_key($arr, array_unique(array_map('strtolower', $arr)));
bucabay
nice and short solution.
powtac
+1  A: 
<?php

function array_not_unique($raw_array) {
    $dupes = array();
    natcasesort($raw_array);
    reset ($raw_array);

   $old_key    = NULL;
     $old_value    = NULL;
  foreach ($raw_array as $key => $value) {
        if ($value === NULL) { continue; }
        if (strcasecmp($old_value, $value) === 0) {
            $dupes[$old_key]    = $old_value;
            $dupes[$key]        = $value;
        }
        $old_value    = $value;
        $old_key    = $key;
    }
return $dupes;
}

$raw_array     = array();
$raw_array[1]    = '[email protected]';
$raw_array[2]    = '[email protected]';
$raw_array[3]    = '[email protected]';
$raw_array[4]    = '[email protected]'; // Duplicate

$common_stuff    = array_not_unique($raw_array);
var_dump($common_stuff);
?>
Srikanth
very smart solution!
bucabay
+13  A: 

You will need to make your function case insensitive to get the "Hello" => "hello" result you are looking for, try this method:

$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'hello', 5=>'U');

// Convert every value to uppercase, and remove duplicate values
$withoutDuplicates = array_unique(array_map("strtoupper", $arr));

// The difference in the original array, and the $withoutDuplicates array
// will be the duplicate values
$duplicates = array_diff($arr, $withoutDuplicates);
print_r($duplicates);

Output is:

Array
(
[3] => Hello
[4] => hello
)
ryanday
Very clean and nice way to do it. Add some comments to it so noob PHP coders can understand : it's possible to write some neat code with that language, and it should be known :-)
e-satis
BTW, if the array is very long, I recommand to had an example using the iterator pattern of the SPL to save memory.
e-satis
You need array_diff_key() otherwise you'll return every value that isn't uppercase in original array.
bucabay
Good idea, I've changed the answer to use better variables and comments. I agree with the iterator also, Mittu how large would the real world array get?
ryanday
Very clean and logical. First get only unique ones and diff with the original array.
presario
@bucabay If he only wants one match of the duplicate values, yes you are right. In his example he gave the array with both "Hello" and "hello" so I only used array_diff(). But Mittu, as bucabay says, if you only want one copy of each duplicate, change array_diff() to array_diff_key()
ryanday
@rday - notice your test subject contains numbers and an uppercase "U" which are all uppercase as far as strtoupper() goes. The returned results are the mixed case values that strtoupper() modified, not the duplicates.If the poster needs both dupes, then the solution john proposed will do the job quite efficiently with a time complexity of O(n) which I believe is the best case here.
bucabay
A: 
function array_not_unique($raw_array) {
    $dupes = array();
    natcasesort($raw_array);
    reset ($raw_array);

   $old_key    = NULL;
     $old_value    = NULL;
  foreach ($raw_array as $key => $value) {
        if ($value === NULL) { continue; }
        if (strcasecmp($old_value, $value) === 0) {
            $dupes[$old_key]    = $old_value;
            $dupes[$key]        = $value;
        }
        $old_value    = $value;
        $old_key    = $key;
    }return $dupes;
}

What shiva srikanth (john) added but with the case insensitive comparison.

bucabay
thanks nice one!
Srikanth