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
WBOYforward
2024-03-19 12:28:12616browse

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);

Output:

[a] => 2
[b] => 2
[c] => 1
[d] => 1

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);

Output:

[a] => 2
[b] => 2
[c] => 1
[d] => 1

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);

Output:

[a] => 2
[b] => 2
[c] => 1
[d] => 1

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);

Output:

[a] => 2
[b] => 2
[c] => 1
[d] => 1

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!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete