0
$input = array(1,1,2,3,2,1,1);
$c = array_count_values($input);
$val = array_search(max($c), $c)
$count = 

This returns 1 as expected (most common). How would I find out how many times it occurs in the array? There are many similar questions, but none of them were interested in finding the count.

echo $val. "ccours". $count . "times";
3
  • Uhhm, wouldn't max($c) be the count? Commented Dec 10, 2011 at 11:40
  • Or, to return both the most common and the count: return array($val => $c[$val]); Commented Dec 10, 2011 at 11:42
  • Oh right, that was a bit silly of me... Commented Dec 10, 2011 at 11:42

1 Answer 1

3

Are you sure you're interpreting the result correctly? Because the function should do exactly, what you need. From the manual:

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

The result should then be:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)
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.