note 71719 added to function.array-unique

From: Date: Wed, 13 Dec 2006 10:45:43 +0000
Subject: note 71719 added to function.array-unique
Groups: php.notes 
Request: Send a blank email to php-notes+get-120569@lists.php.net to get a copy of this message
What if I only want the duplicated items, not the unique ones? I tried to do it by using array_diff of an array and its unique, but it did not work (any help?); so I made this function with a relatively good efficiency: function array_repeated($array) { if(!is_array($array)) return false; $repeated_values = Array(); $array_unique = array_unique($array); if(count($array)-count($array_unique)) { for($i=0;$i<count($array);$i++) { if(!array_key_exists($i, $array_unique)) $repeated_values[] = $array[$i]; } } return $repeated_values; } This function is based on the behaviour of the array_unique function that mantains the indexes of the array when deleting the item. ---- Server IP: 64.71.164.2 Probable Submitter: 62.15.153.234 ---- Manual Page -- http://www.php.net/manual/en/function.array-unique.php Edit -- https://master.php.net/note/edit/71719 Del: integrated -- https://master.php.net/note/delete/71719/integrated Del: useless -- https://master.php.net/note/delete/71719/useless Del: bad code -- https://master.php.net/note/delete/71719/bad+code Del: spam -- https://master.php.net/note/delete/71719/spam Del: non-english -- https://master.php.net/note/delete/71719/non-english Del: in docs -- https://master.php.net/note/delete/71719/in+docs Del: other reasons-- https://master.php.net/note/delete/71719 Reject -- https://master.php.net/note/reject/71719 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120569) next »