Computer >> Computer tutorials >  >> Programming >> PHP

array_count_values() function in PHP


The array_count_values() function returns an array with the number of occurrences for each value. It returns an associative array. The returned array has keys as the array’s values, whereas values as the count of the passed values.

Syntax

array_count_values(arr)

Parameters

  • arr − The array for which we want to count the values.

Return

The array_count_values() function returns an associative array. The returned array has keys as the array’s values, whereas values as the count of the passed values.

Example

<?php
   $arr = array("Laptop","Keyboard","Mouse","Keyboard","Keyboard", "Mouse","Keyboard");
   print_r(array_count_values($arr));
?>

Output

Array
(
   [Laptop] => 1
   [Keyboard] => 4
   [Mouse] => 2
)