Home  >  Article  >  Backend Development  >  How to query the total number of elements in a two-dimensional array in php

How to query the total number of elements in a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-19 10:08:09782browse

In PHP language, two-dimensional array is a common data structure, which consists of multiple ordered one-dimensional arrays. When operating on a two-dimensional array, we may need to count the total number of occurrences of an element in the array. Here is a way to quickly query the total number of elements in a two-dimensional array.

First, we can use the foreach loop in the PHP language to traverse the two-dimensional array. When traversing a one-dimensional array, we can use the count() function to get the length of the array and thus traverse all elements in the array.

For example, we have a two-dimensional array as follows:

$students = array(
    array("name" => "Tom", "score" => 80),
    array("name" => "Jerry", "score" => 90),
    array("name" => "Lucy", "score" => 70),
    array("name" => "John", "score" => 85),
    array("name" => "Andy", "score" => 90)
);

If we want to query the total number of elements with a score of 90 in the array, we can use the following code:

$count = 0;
foreach ($students as $student) {
    if ($student['score'] == 90) {
        $count++;
    }
}
echo $count; // 输出2

In the code, we use a foreach loop to traverse the two-dimensional array $students. In the loop, we use the if statement to determine whether the score value of each element is 90. If so, the value of the accumulated counter is added.

When the loop ends, we can use the echo statement to output the value of the counter, which is the total number of elements with a score of 90 in the array. In the above code, the output result is 2, which is the total number of elements in the array with a score of 90.

In addition to the above methods, we can also use the array_column() function to obtain all values ​​of the specified key in the two-dimensional array, and use the count() function to calculate the length of the array to count the total number of elements.

For example, we can use the following code to query the total number of elements with a score of 90 in the above array:

$count = count(array_keys(array_column($students, 'score'), 90));
echo $count; // 输出2

In the code, we first use the array_column() function to get all the score keys in the $students array value, and then use the array_keys() function to obtain all key names with a value of 90 in the array. Finally, use the count() function to calculate the length of the array, which is the total number of elements with a score of 90. In the above code, the output result is also 2.

To sum up, we can use two methods to quickly query the total number of specified elements in a two-dimensional array. Whether using the foreach loop or the array_column() function, it can provide us with convenient data manipulation methods.

The above is the detailed content of How to query the total number of elements in a two-dimensional array in php. 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