PHP array_count_values() Function

The array_count_values() function returns an array where the keys are the original array's values, and the values is the number of occurrences. In other words, we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array.

Syntax


array_count_values(array);  
        

Parameters

Parameter Description Is Compulsory
Array Specifying the array. Compulsory

Return Type

Returns an associative array, where the keys are the original array's values, and the values are the number of occurrences. This function is introduced in PHP 4


PHP Example


<?php  
$details = array(3, "Hii", 3, "World", "Hii");  
print_r(array_count_values($details));  
?>  
    

Output

Array( [3] => 2 [Hii] => 2 [World] => 1 )

PHP Example


<?php  
function Counting($array)  
{  
    return(array_count_values($array));  
}  
              
    $array = array("php", "T", "php", "php", "Point", "T");  
    print_r(Counting($array));  
               
?>  
        

Output

Array( [php] => 3 [T] => 2 [Point] => 1 )

PHP Example


<?php  
$empname=array("Ajay","Sid","Rahul","Ajay","Ashish","Sid");  
print_r(array_count_values($empname));  
?>  
                 

Output

Array ( [Ajay]=>2 [Sid]=>2 [Rahul]=>1 [Ashish]=>1 )

PHP Example


<?php  
$a=array("Cricket","Hockey","Football","Hockey");  
print_r(array_count_values($a));  
?>           
        

Output

Array ( [Cricket] => 1 [Hockey] => 2 [Football] => 1 )