1

I have a script that puts a bunch of variables (in this case random letters A through D) into an array, counts the frequency of these variables, finds the highest frequency and then finds the key that matches this frequency.

$answerlist = array($a1, $a2, $a3, $a4,);
$count = array_count_values($answerlist);
$high_value = max($count);
$high_key = array_search($high_value, $count); 
print_r ($high_key);

However in a case where there are 2 equal highest values, array_search only returns the first key. Is there a way to return both?

jack
  • 393
  • 2
  • 5
  • 16
  • possible duplicate of [PHP - Array search for multiple values](http://stackoverflow.com/questions/1212605/php-array-search-for-multiple-values) – Amal Murali Jan 31 '14 at 16:21

1 Answers1

3

This should do it:

$high_keys = array_keys($count, $high_value);

From the array_search docs:

If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.

jszobody
  • 28,495
  • 6
  • 61
  • 72