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. Quote 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; } ?> Quote 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. Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.