Home >>PHP Array Functions >PHP array_count_values() Function
PHP array_count_values() Function is used to count all the values present inside a given array. It is used to calculate the frequency of all of the elements of a given input array. It accepts only a single parameter $arraythat is the array for which the count value is to be calculated. It returns an associative array containing key-value pairs in which keys are the elements of the array and values are the frequency of those elements in the given array.
Syntax:
array_count_values($array);
Parameter | Description |
---|---|
array | This is a required parameter. This parameter defines the array to count values of. |
Here is an example of array_count_values() function in PHP:
<html> <body> <pre> <?php $a=array("Abhi","Jerry","Dog","Abhi","Dog","Sunday"); print_r(array_count_values($a)); ?> </pre> </body> </html>
Array ( [Abhi] => 2 [Jerry] => 1 [Dog] => 2 [Sunday] => 1 )
Example 2:
<html> <body> <pre> <?php $a=array("a","b","c","d","e","f","h","a","c","a","e","b","e","h","c"); print_r(array_count_values($a)); ?> </pre> </body> </html>
Array ( [a] => 3 [b] => 2 [c] => 3 [d] => 1 [e] => 3 [f] => 1 [h] => 2 )