In this tutorial, you shall learn about PHP array_count_values() function which can count the occurrences of all values in an array, with syntax and examples.
PHP array_count_values() Function
PHP array_count_values() function counts the occurrences of all values in an array and returns an associative array formed by the unique value of input array as keys, and the number of their occurrences in the array as values.
In this tutorial, we will learn the syntax of array_count_values(), and how to use array_count_values() to count the occurrences of values in the array, covering different scenarios based on array type and arguments.
Syntax of array_count_values()
The syntax of PHP array_count_values() function is
array_count_values ( array $input ) : array
where
| Parameter | Description |
|---|---|
| input | Occurrences of values in this input array are counted. |
Return Value
The array_count_values() function returns an array formed with unique values as keys, and their number of occurrences as values.
Warnings
array_count_values() expects elements of the array have to be either string or integer. So, if an element is neither string nor integer, the function throws a warning.
Examples
1. Count the occurrences of values in array $input
In this example, we will take a array and count the occurrences of values in the array.
PHP Program
<?php
$input = array(41, 'a', 41, 'a', 'b');
$result = array_count_values($input);
print_r($result)
?>
Output
