1

I was wondering how I would go about displaying the most common values in an array, so far my script gives the single most common value but what if I wanted the 5 most common?

function array_most_common($array) {
   $counted = array_count_values($array); 
   arsort($counted); 
   return(key($counted));       
}

echo array_most_common($array);

Many thanks.

1 Answer 1

2

Seems like you have it. I'd simply modify your function to return the the whole array so you can perform whatever logic you want afterwards:

function array_most_common($array) {
  return arsort(array_count_values($array));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.