What is the PHP array_count_values() Function?

The PHP array_count_values() Function is used to count each element (how many times each element contains into the array) of an array.


Syntax of the array_count_values()

array_count_values(array)

The array count values function takes only one parameter that is an array.


Example:

<?php
// The following $names array contains -
// 3 times - John
// 2 times - Mark
// 1 time - Other names
$names = array("John","Mark","Barray","John","Adam","Mark","John");
$result = array_count_values($names);

echo "<pre>";
var_dump($result);
echo "</pre>";
?>
Browser Output
array(4) {
  ["John"]=>
  int(3)
  ["Mark"]=>
  int(2)
  ["Barray"]=>
  int(1)
  ["Adam"]=>
  int(1)
}

Leave a Reply

Your email address will not be published. Required fields are marked *