Re: Antithesis to array_unique
| From: | David Nicholson | Date: | Wed, 09 Jul 2003 14:08:29 +0000 |
| Subject: | Re: Antithesis to array_unique | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-154553@lists.php.net to get a copy of this message | ||
Hello,
This is a reply to an e-mail that you wrote on Wed, 9 Jul 2003 at 14:55,
lines prefixed by '>' were originally written by you.
> Having STFM I am wondering if anyone knows of a quick antithesis to
> array_unique? We have an array in which we need to return all
> duplicate
> values instead of the non-duplicates.=20
How about:
$array1 = array("dupe","dupe","another item");
$valuecount = array();
foreach($array1 as $value){
if(isset($valuecount[$value])){
$valuecount[$value]++;
} else {
$valuecount[$value] = 1;
}
}
$dupes = array();
foreach($valuecount as $value=>$count){
if($count>1){
array_push($dupes,$value);
}
}
// $dupes now contains all duplicate values.
There may be a quicker way of doing it though...
David.
--
phpmachine :: The quick and easy to use service providing you with
professionally developed PHP scripts :: http://www.phpmachine.com/
Professional Web Development by David Nicholson
http://www.djnicholson.com/
QuizSender.com - How well do your friends actually know you?
http://www.quizsender.com/
(developed entirely in PHP)