Current Location: Home> Function Categories> array_count_values

array_count_values

Count the number of values in the array
Name:array_count_values
Category:Array
Programming Language:php
One-line Description:Used to count the number of times all values appear in the array.

array_count_values function

Applicable to PHP version

This function is available since PHP 4.0.0.

Function description

The array_count_values() function is used to count the occurrences of all values in an array and return an associative array. The key of the returned array is the value in the original array, and the value is the number of times the value appears in the original array.

Function Syntax

 <span class="fun">array_count_values(array $array): array</span>

parameter

  • array (array) - an array that requires statistics.

Return value

Returns an associative array where the key of the array is the value of the input array, and the value is the number of times the value appears in the input array.

Example

 <?php
$array = array(1, "apple", 2, 3, "apple", 1, 3, "banana", "apple");
$result = array_count_values($array);
print_r($result);
?>

Description of sample code

In this example, the array contains multiple duplicate values, such as the number 1 and the string "apple". After calling the array_count_values() function, the returned result will be an associative array, indicating the number of times each element appears in the original array. The output result is:

 Array ( [1] => 2 [apple] => 3 [2] => 1 [3] => 2 [banana] => 1 )
Similar Functions
Popular Articles