PHP | array_count_values() Function
The array_count_values() is an inbuilt function in PHP which 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 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 parameter and values are the frequency of these elements in an array.
Note: If the element is not a string or integer then an E_WARNING is thrown.
Examples:
Input : array = ("Geeks", "for", "Geeks", "Geeks", "Welcome", "for")
Output :
Array
(
[Geeks] => 3
[for] => 2
[Welcome] => 1
)
Input : array = (1, 1, 2, 3 , 1 , 2 , 4, 5)
Output :
Array
(
[1] => 3
[2] => 2
[3] => 1
[4] => 1
[5] => 1
)
Below program illustrates the working of array_count_values() function in PHP:
<?php // PHP code to illustrate the working // of array_count_values() function function Counting($array){ return(array_count_values($array)); } // Driver Code $array = array("Geeks", "for", "Geeks", "Geeks", "Welcome", "for"); print_r(Counting($array)); ?> |
Output:
Array
(
[Geeks] => 3
[for] => 2
[Welcome] => 1
)
Reference: http://php.net/manual/en/function.array-count-values.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- p5.js | arc() Function
- CSS | rgb() Function
- p5.js | box() Function
- D3.js | d3.lab() Function
- PHP | each() Function
- PHP | ord() Function
- D3.js | d3.set.add() Function
- D3.js | d3.map.set() Function
- D3.js | d3.hcl() Function
- CSS | hsl() Function
- PHP | abs( ) Function
- p5.js | nfc() function
- p5.js | nf() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



