PHP is a widely used scripting language used for web development, command line interface and embedded application development. PHP is easy to learn, easy to use, safe and efficient, so it is widely used in web development.
In PHP, a function is a reusable block of code that performs a specific task. There are many functions available in the PHP function library, one of which is very useful is array_count_values().
array_count_values() function is a PHP function used to count the number of occurrences of each value in an array. In this article, we will take an in-depth look at the usage and examples of array_count_values() function.
Usage
The syntax of the array_count_values() function is as follows:
array_count_values(array $array): array
This function accepts an array as input, and Returns an associative array whose keys represent unique values in the original array and whose values represent the number of occurrences of that value in the original array.
Parameter description:
array: required. An array to count the number of occurrences of each value.
Return value:
array: an associative array, the key of the array represents the unique value in the original array, and the key value represents the number of occurrences of the value in the original array.
Examples
The following are some examples about the array_count_values() function:
Example 1:
$colors = array("red", "blue", "green", "blue", "yellow", "red", "green", "red"); $color_count = array_count_values($colors); print_r($color_count);
Output:
Array ( [red] => 3 [blue] => 2 [green] => 2 [yellow] => 1 )
In In the above example, we create an array $colors containing multiple repeated values and pass it to the array_count_values() function. The function returns an associative array $color_count that contains the number of occurrences of each value in the original array.
Example 2:
$text = "The quick brown fox jumps over the lazy dog"; $word_array = explode(" ", $text); $word_count = array_count_values($word_array); print_r($word_count);
Output:
Array ( [The] => 1 [quick] => 1 [brown] => 1 [fox] => 1 [jumps] => 1 [over] => 1 [the] => 1 [lazy] => 1 [dog] => 1 )
In the above example, we created a string $text containing words and used the explode() function to It is split into word array $word_array. Next, we pass $word_array to the array_count_values() function, which returns an associative array $word_count containing the number of occurrences of each word in $word_array.
Notes
The following are some notes on the array_count_values() function:
Conclusion
In this article, we learned about the array_count_values() function in PHP arrays. This function can help us quickly count the number of occurrences of each value in an array. Whether you are working with PHP in web development or in other fields, this function is very useful.
The above is the detailed content of PHP function encyclopedia array_count_values(). For more information, please follow other related articles on the PHP Chinese website!