note 81701 deleted from function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Nov 2008 14:52:27 +0000 |
| Subject: | note 81701 deleted from function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146216@lists.php.net to get a copy of this message | ||
Note Submitter: xp_sim at hotmail dot com
----
Ray dot Paseur at SometimesUsesGmail dot com :
You might also want to try array_count_values. This function returns the number of repetitions for
each value in the array.
Then, you sort the resulting array in descending order and parse it using a "foreach" to
get all the values that are present more than once in the original array.
Ex. :
<?php
//From your code
$raw_array = array();
$raw_array[1] = 'abc@xyz.com';
$raw_array[2] = 'def@xyz.com';
$raw_array[3] = 'ghi@xyz.com';
$raw_array[4] = 'abc@xyz.com'; // Duplicate
//Count the no. of repetitions
$count_by_value = array_count_values( $raw_array );
//Sort in asc. order
natcasesort( $count_by_value )
//Reverse order (desc.)
$count_desc = array_reverse( $count_by_value );
$not_uniques = array();
foreach( $count_desc as $orignal_value => $count){
//Exit loop if only uniques remain
if( $count == 1 ){ break; }
//Add current value to the output array
$not_uniques[] = $orignal_value;
}
print_r($not_uniques);
?>
What do you think ?