149 of 410 menu

The array_count_values Function

The array_count_values function counts the occurrences of all values in an array. It returns an associative array where the keys are the array elements, and the values are their count in the array.

Syntax

array_count_values(array $array): array

Example

Let's count how many times each element appears:

<?php $arr = ['a', 'a', 'a', 'b', 'b', 'c']; var_dump(array_count_values($arr)); ?>

Code execution result:

['a'=>3, 'b'=>2, 'c'=>1]

See Also

  • the count function,
    which counts the number of elements in an array
byenru