array_count_values()
array_count_values() — Counts the occurrences of each distinct value in an array.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
array_count_values() returns an array using the values of array (which must be ints or strings) as keys and their frequency in array as values.
Signature
array_count_values(array $array): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $array | array | The array of values to count |
Return value
Returns an associative array of values from array as keys and their count as value.
Example
A runnable snippet:
$array = array(1, "hello", 1, "world", "hello"); print_r(array_count_values($array));
Output:
Array ( [1] => 2 [hello] => 2 [world] => 1 )
See also
count(), array_unique(), array_values(), count_chars()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_count_values() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.