What is the use of array_count_values() function in PHP ?
In this article we will discuss about array_count_values() function in PHP
The array_count_values() function is used to count all the values inside an array. 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 array_count_values( $array )
Parameters: This function accepts a single parameter $array. This parameter is the array for which we need to calculate the count of values present in it.
Return Value: This function returns an associative array with key-value pairs in which keys are the elements of the array passed as parameters and values are the frequency of these elements in an array.
Example: PHP Program to count values in an array.
PHP
<?php // Array of subjects with 4 elements $array1 = array ( "Python" , "c/cpp" , "php" , "java" ); // Count values in an array print_r( array_count_values ( $array1 )); ?> |
Output
Array ( [Python] => 1 => 1 => 1 => 1 )
Example 2:
PHP
<?php // Array of subjects with 8 elements $array1 = array ( "Python" , "c/cpp" , "php" , "java" , "R programming" , "c/cpp" , "php" , "java" ); // Count values in an array print_r( array_count_values ( $array1 )); ?> |
Output
Array ( [Python] => 1 => 2 => 2 => 2 [R programming] => 1 )
Please Login to comment...