How to count the number of occurrences of each value in an array using the array_count_values ​​function in PHP

WBOY
Release: 2023-06-26 14:14:02
Original
1435 people have browsed it

In PHP, we often need to perform various operations on arrays. Counting the number of occurrences of each value in an array is one of the common requirements. PHP provides a simple and efficient function - array_count_values, which can easily complete this task.

The syntax of the array_count_values ​​function is as follows:

array_count_values(array $array)

This function receives an array parameter and returns a new array containing each element in the original array. The number of times the value appears.

Below, let's look at some examples showing how to use the array_count_values ​​function.

Example 1:

Suppose we have an array containing multiple numbers:

$numbers = array(1, 2, 3, 4, 5, 1, 2, 3, 1, 2);

We can use the array_count_values ​​function to count the number of occurrences of each number:

$result = array_count_values($numbers);

This At that time, the content of the $result array is as follows:

Array
(

[1] => 3
[2] => 3
[3] => 2
[4] => 1
[5] => 1
Copy after login

)

As you can see, the number of occurrences of each number in the array is counted. Out.

Example 2:

If we have an array containing strings:

$fruits = array("apple", "banana", "orange", "apple ", "apple", "orange");

We can also use the array_count_values ​​function to count the number of occurrences of each string:

$result = array_count_values($fruits);

At this time, the contents of the $result array are as follows:

Array
(

[apple] => 3
[banana] => 1
[orange] => 2
Copy after login

)

You can also see that each string in the array The number of occurrences is counted.

It should be noted that the array_count_values ​​function can only count the values ​​in the array, not the keys. If statistical keys are needed, other methods need to be used.

Summary:

Use the array_count_values ​​function to count the number of occurrences of each value in an array reliably and efficiently. This function is very simple and only needs to be passed an array parameter to complete the task. Please note during use that it can only count the values ​​in the array, not the keys.

The above is the detailed content of How to count the number of occurrences of each value in an array using the array_count_values ​​function in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!