Forum Moderators: coopster
Usually you need to get unique values from an array, but I actually sort of want to do the opposite to that.
I need to get all the values from an array that appear more than once, and discard the rest, so like:
(orange, banana, strawberry, strawberry, orange, pineapple, orange, orange)
becomes
(orange, strawberry)
Any ideas how I would do that?
Cheers!
YF
$array = array('orange', 'banana', 'strawberry', 'strawberry', 'orange', 'pineapple', 'orange', 'orange');
$countedvals = array_count_values($array);
$filtered = array_filter($countedvals, 'more_than_one_filter');
$filtered = array_keys($filtered);
echo '<pre>';
print_r($filtered);
echo '</pre>';
function more_than_one_filter($var){
return ($var > 1);
}
This is a bit round-a-bout, maybe someone else here can suggest a more direct method.