PHP array_count_values()

Last Updated: February 10, 2022

array_count_values: Count the frequency of each element of the array. Elements should be either integer or string.

Syntax

 array_count_values(array $array): array

Supports (PHP 4, PHP 5, PHP 7, PHP 8)

Throws E_WARNING for every element which is not string or int.

Parameters

Parameter Description
array Required. Target array

Return Value

array with a frequency count

Example

<?php

$numbers= array(1,1,2,2,'One',"Two","Two","Three","Four");
print_r(array_count_values($numbers));

?>

Output

Array ( 
	[1] => 2 
	[2] => 2 
	[One] => 1 
	[Two] => 2 
	[Three] => 1 
	[Four] => 1 
) 
Test