rosenrosen Posted October 20, 2008 Share Posted October 20, 2008 I'm looking for a way to list all the duplicated values in an array (like the opposite of array_unique but not quite). Given an array of ("a", "b", "c", "a", "d", "e", "d") I want to return ("a","d"). There has to be an easy answer but its eluding me. Thanks. Link to comment https://forums.phpfreaks.com/topic/129302-solved-find-duplicate-array-values/ Share on other sites More sharing options...
trq Posted October 20, 2008 Share Posted October 20, 2008 One idea. <?php function get_unique($arr) { $ret = array(); $counted = array_count_values($arr); foreach ($counted as $k => $v) { if ($v > 1) { $ret[] = $k; } } return $ret; } ?> Link to comment https://forums.phpfreaks.com/topic/129302-solved-find-duplicate-array-values/#findComment-670348 Share on other sites More sharing options...
.josh Posted October 20, 2008 Share Posted October 20, 2008 There are several comments in the array_unique entry on php.net that tackle getting the duplicate ones. Link to comment https://forums.phpfreaks.com/topic/129302-solved-find-duplicate-array-values/#findComment-670349 Share on other sites More sharing options...
trq Posted October 20, 2008 Share Posted October 20, 2008 Actually, thinking about it, might need some adjusting. <?php function get_unique($arr) { $ret = array(); $counted = array_count_values($arr); foreach ($counted as $k => $v) { if ($v > 1) { if (!in_array($k,$ret)) { $ret[] = $k; } } } return $ret; } ?> Still not tested. Link to comment https://forums.phpfreaks.com/topic/129302-solved-find-duplicate-array-values/#findComment-670353 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.