Home > Backend Development > PHP Tutorial > How to count the number of values ​​in an array in PHP

How to count the number of values ​​in an array in PHP

WBOY
Release: 2024-03-19 12:30:02
forward
618 people have browsed it

php Editor Xiaoxin will introduce to you how to use PHP to count the number of each value in an array. In PHP, this function can be achieved by using the array_count_values() function. This function returns an associative array containing the number of occurrences of each value in the array. By traversing this associative array, you can get the number of each value in the array. This method is simple and efficient, suitable for various types of arrays, and provides you with a convenient and fast statistical method.

PHP counts the number of each value in the array

In php, there are several methods to count the number of each value in an array:

1. Use array_count_values ​​function

array_count_values The function can count the number of occurrences of each value in the array and return an array of key-value pairs, where the key is the value and the value is the number of occurrences.

$arr ​​= ["a", "b", "c", "a", "b", "d"];

$counts = array_count_values($arr);

// Output $counts array
print_r($counts);
Copy after login

Output:

[a] => 2
[b] => 2
[c] => 1
[d] => 1
Copy after login
Copy after login
Copy after login
Copy after login

2. Use count function

count The function can count the number of elements in the array. By using nested loops and the isset function, you can count the number of occurrences of each value in an array.

$arr ​​= ["a", "b", "c", "a", "b", "d"];

$counts = [];

foreach ($arr as $value) {
if (!isset($counts[$value])) {
$counts[$value] = 0;
}
$counts[$value] ;
}

// Output $counts array
print_r($counts);
Copy after login

Output:

[a] => 2
[b] => 2
[c] => 1
[d] => 1
Copy after login
Copy after login
Copy after login
Copy after login

3. Use foreach loop

Use a foreach loop to iterate through each value in the array and store the number of occurrences in an associative array.

$arr ​​= ["a", "b", "c", "a", "b", "d"];

$counts = [];

foreach ($arr as $value) {
$counts[$value] = isset($counts[$value]) ? $counts[$value] 1 : 1;
}

// Output $counts array
print_r($counts);
Copy after login

Output:

[a] => 2
[b] => 2
[c] => 1
[d] => 1
Copy after login
Copy after login
Copy after login
Copy after login

4. Use group_by function (PHP 7)

PHP 7 introduced the group_by function, which can group elements in an array, where the key is the key of the group and the value is the element in the group.

$arr ​​= ["a", "b", "c", "a", "b", "d"];

$counts = group_by($arr)->map(function ($group) {
return count($group);
});

// Output $counts array
print_r($counts);
Copy after login

Output:

[a] => 2
[b] => 2
[c] => 1
[d] => 1
Copy after login
Copy after login
Copy after login
Copy after login

Method of choosing

Which method to choose depends on the size of the array and the PHP version used. For small arrays, using the array_count_values function is usually fastest. For medium to large arrays, using the count function or the foreach loop may be a better choice. In PHP 7, the group_by function provides a concise way to group and count array values.

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

source:lsjlt.com
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template