Understanding the PHP Function "array_count_values"
The array_count_values function in PHP is an essential tool for counting the occurrence of each value in an array. It returns an associative array that contains
The PHP array_count_values() function counts how many times each value appears in an array. It returns a new associative array in which the keys are the distinct values from the input array and the values are how often each one occurred. This is the fastest way to build a frequency table (a histogram of values) without writing a loop yourself.
This chapter covers the syntax, the rules for which values can be counted, common gotchas, and practical recipes such as finding the most frequent element.
Syntax
array_count_values(array $array): array| Parameter | Description |
|---|---|
$array | The input array whose values you want to count. Only int and string values are counted. |
The function returns an associative array of value => count pairs. It does not modify the original array.
Basic example
Pass an array of values, and you get back each distinct value paired with its count:
Output:
Array
(
[red] => 2
[green] => 1
[blue] => 2
[yellow] => 1
)"red" and "blue" each appear twice, while "green" and "yellow" appear once. The original array keys are ignored entirely — only the values matter.
How integer and string keys interact
Because PHP array keys can only be integers or strings, array_count_values() only counts int and string values. There is one subtle consequence: a numeric string like "1" and the integer 1 are treated as the same key, so they are counted together.
<?php
$mixed = array(1, "1", 1, "hello", "hello");
print_r(array_count_values($mixed));
?>Output:
Array
(
[1] => 3
[hello] => 2
)The integer 1 and the string "1" collapse into a single key 1 with a count of 3. This mirrors how PHP normalizes array keys everywhere.
Values that cannot be counted
Any value that is not an int or a string — null, booleans, floats, arrays, or objects — cannot be used as a key. PHP skips it and emits a warning rather than counting it:
<?php
$values = array("a", "b", null, 3.5, "a");
print_r(@array_count_values($values));
?>Output:
Array
(
[a] => 2
[b] => 1
)Only the string values are counted; null and the float 3.5 are dropped (the real call raises "Can only count string and integer values" warnings — the @ above is just to keep the output clean for the demo). Cast or filter your data first if you need to count other types.
Practical recipe: find the most frequent value
A frequency table makes it trivial to find the mode (the most common element). Sort the counts in descending order and read the first key:
<?php
$votes = array("yes", "no", "yes", "yes", "no", "maybe");
$counts = array_count_values($votes);
arsort($counts); // sort by count, highest first, keeping keys
$winner = array_key_first($counts);
echo "Winner: $winner ({$counts[$winner]} votes)";
?>Output:
Winner: yes (3 votes)You can also feed the result straight into array_sum() to confirm the total, or into max() to grab the highest count.
Counting words in a string
A common use case is building a word-frequency map. Split the string into words with explode(), then count:
<?php
$text = "the cat sat on the mat the cat ran";
$words = explode(" ", $text);
print_r(array_count_values($words));
?>Output:
Array
(
[the] => 3
[cat] => 2
[sat] => 1
[on] => 1
[mat] => 1
[ran] => 1
)When to use it
Reach for array_count_values() whenever you need a histogram of values: tallying votes, finding duplicates, building tag clouds, or detecting the most/least common entries. It is faster and clearer than a manual foreach loop with isset() checks, and it removes a frequent source of off-by-one bugs.
Related functions
array_unique()— remove duplicate values (when you only care which values exist, not how many times).array_keys()— pull out just the distinct values once they are keys.array_sum()— total the counts.arsort()— sort the resulting frequency table by count.