PHP array_​count_​values Function



array_​count_​values() is an another PHP pre built / default array function, This function is basically allows user to count all the values of an array and returns an array in which the values of array as keys and their frequency in array as values.

Syntax of array_​count_​values() :

array_count_values(array)

Parameter :
array (required) : The testing array to count values of.

Return Type / Values :
It returns an associative array, in which the keys are the array’s values, and the values are the number of frequency.

Example 1 :

<?php
  
// Code to illustrate the working of PHP array_count_values() function

function ewacounting($array_count){
    return(array_count_values($array_count));
}
  
$array_count = array("EWA", "Welcome", "my", "Website", "EWA", "on");
print_r(ewacounting($array_count));
  
?>

Output will be :

Array 
	( 
		[EWA] => 2 
		[Welcome] => 1 
		[my] => 1 
		[Website] => 1 
		[on] => 1 
	)

Example 2 :

<?php
	$items = array("1", "2", "X", "3", "X", "2", "Y", "4", "3", "5", "3");
	print_r(array_count_values($items));
?>

Output will be :

Array 
	( 
		[1] => 1 
		[2] => 2 
		[X] => 2 
		[3] => 3 
		[Y] => 1 
		[4] => 1 
		[5] => 1 
	)

Reference: https://www.php.net/manual/en/function.array-count-values.php