Forum Moderators: coopster

Message Too Old, No Replies

I want to do array_unique BUT IN REVERSE!

how to keep values in an array that appear more than "x" times?

         

yongfook

6:16 am on Aug 8, 2005 (gmt 0)

10+ Year Member



hello everyone. This is a simple problem I think, but I can't work it out.

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

willybfriendly

6:30 am on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like

SELECT * FROM 'favorites' WHERE fruit = 'orange' OR fruit = 'strawberry'

WBF

yongfook

6:56 am on Aug 8, 2005 (gmt 0)

10+ Year Member



no I think you've misunderstood. I'm not getting my data from MySQL or anything.

I just have a simple data array with words. There are many words which are the same. I want to shrink the array to contain ONLY those words that occur one or more times within the array.

mincklerstraat

8:27 am on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$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.

yongfook

8:40 am on Aug 8, 2005 (gmt 0)

10+ Year Member



thanks! I'll try that out.

Funny, it really seems like there should be a php array function for this already... :(