Zugzwangle Posted July 21, 2010 Share Posted July 21, 2010 how would you write a function to find values from an array that are NOT unique? 'array_unique' finds unique items... Link to comment https://forums.phpfreaks.com/topic/208379-not-unique/ Share on other sites More sharing options...
Mchl Posted July 21, 2010 Share Posted July 21, 2010 array_diff($yourArray,array_unique($yourArray)); Link to comment https://forums.phpfreaks.com/topic/208379-not-unique/#findComment-1088934 Share on other sites More sharing options...
Zugzwangle Posted July 21, 2010 Author Share Posted July 21, 2010 I have tried the below: $uniqueArr = array_diff($thisArray, array_unique($thisArray)); echo print_r($uniqueArr, true); // returns "Array ( )" I have tried to echo print_r, on the $uniqueArr, but it simply returns "Array ( )". There are definitely duplicates within $thisArray. What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/208379-not-unique/#findComment-1088942 Share on other sites More sharing options...
Mchl Posted July 21, 2010 Share Posted July 21, 2010 Arrgh... that's because array_unique doesn't do what we thought it was doing. It removes duplicates, not returns unique values. <?php $a1 = array('a','b','b','c','d','d'); $a2 = array_count_values($a1); $a3 = array(); foreach($a2 as $key => $value) { if($value > 1) { $a3[] = $key; } } print_r($a3); Link to comment https://forums.phpfreaks.com/topic/208379-not-unique/#findComment-1088947 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.