PHP Tutorial - PHP array_count_values() function counts the values in an array






Definition

The array_count_values() function counts the values of an array.

Syntax

PHP array_count_values() function has the following syntax.

array_count_values(array)

Parameter

  • array - Required. Array to count

Return

It returns an associative array, where the keys are the original array's values, and the values are the number of occurrences.





Example 1

Count all the values of an array


<?php
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>

The code above generates the following result.

Example 2

Count an associative array


<?php
$age=array("PHP"=>"5","Python"=>"7","Java"=>"3");
print_r(array_count_values($age));
?>

The code above generates the following result.