array_count_values

array array_count_values(array values) 

Counts the occurrences of all elements in the argument and returns an associative array of the resulting frequencies.

Returns:

Associative array; NULL if given an invalid argument

Description:

Passing an array to array_count_values() returns an associative array in which all the unique elements are counted and returned with the values as the keys and the frequencies as the values.

Version:

PHP 4 since 4.0b4

Example:

Display the number of times a given element occurs in an array
$scores = array('A', 'A', 'C', 'B', 'A', 'C', 'C', 'B', 'A'); 
$grades = array_count_values($scores); 
echo "There were $grades[A] A's, $grades[B] B's, and $grades[C] C's"; 

Output: There were 4 A's, 2 B's, and ...

Get PHP Functions Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.