Home  >  Article  >  Backend Development  >  PHP function encyclopedia array_count_values()

PHP function encyclopedia array_count_values()

王林
王林Original
2023-06-20 12:01:301783browse

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:

  • This function is not case-sensitive, so values ​​considered to be the same will be treated as for the same key.
  • This function is data type sensitive, so the same value will be cast before calculation. For example, the string "2" will be treated as the number 2.
  • If the parameter passed to the function is not an array, the function will return false.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn