note 81701 added to function.array-unique
| From: | xp_sim at hotmail dot com | Date: | Mon, 10 Mar 2008 12:55:32 +0000 |
| Subject: | note 81701 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136901@lists.php.net to get a copy of this message | ||
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 ?
----
Server IP: 66.207.199.35
Probable Submitter: 207.96.178.150
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/81701
Del: integrated -- https://master.php.net/note/delete/81701/integrated
Del: useless -- https://master.php.net/note/delete/81701/useless
Del: bad code -- https://master.php.net/note/delete/81701/bad+code
Del: spam -- https://master.php.net/note/delete/81701/spam
Del: non-english -- https://master.php.net/note/delete/81701/non-english
Del: in docs -- https://master.php.net/note/delete/81701/in+docs
Del: other reasons-- https://master.php.net/note/delete/81701
Reject -- https://master.php.net/note/reject/81701
Search -- https://master.php.net/manage/user-notes.php