Home  >  Article  >  Backend Development  >  How to query multi-dimensional array in php and output this row

How to query multi-dimensional array in php and output this row

PHPz
PHPzOriginal
2023-04-19 10:07:10427browse

With the development of the Internet, PHP has increasingly become one of the most popular server-side scripting languages ​​and is widely used in the development of various web applications. The application of arrays in PHP is extremely common, and multidimensional arrays are an important part of it. When using multi-dimensional arrays, we often need to perform query operations and obtain the required data by outputting this row. This article will use examples to introduce how to perform multi-dimensional array queries in PHP and output the method of this line.

1. What is a multidimensional array?

Multidimensional array refers to an array containing another array. Simply put, an array contains one or more arrays. For example:

$food = array(
array("name"=>"apple", "color"=>"red"),
array("name"=>"banana", "color"=>"yellow"),
array("name"=>"orange", "color"=>"orange")
);

In the above example, $food is a multi-dimensional array, which contains three one-dimensional arrays. Each one-dimensional array contains the name and color attributes of the fruit.

2. Query of multi-dimensional arrays

When using multi-dimensional arrays, we often need to perform query operations in order to retrieve the required data from the array. For example, in the above example, we need to query the names of all fruits whose color is red. At this time, we can use the foreach loop statement to query.

foreach($food as $item){
if($item["color"]=="red"){
echo $item["name"];
}
}

In the above code, we use a foreach loop to traverse all elements in the array $food. When a red fruit is found, its name is output.

Of course, the above method is only applicable when the position of the data to be queried in the array is known. More complex queries and operations that output rows require more advanced methods.

3. Multi-dimensional array query output this row

Below, we will use an example to introduce how to perform multi-dimensional array query in PHP and output this row.

Example: Suppose we have an array containing multiple student information. Each student information contains the student's name, student number, and course information, including course name, grades, etc., as shown below:

$students = array(
array("name"=>"张三", "number"=>"001", "courses"=>array(array("courseName"=>"语文", "score"=>89),array("courseName"=>"数学", "score"=>95))),
array("name"=>"李四", "number"=>"002", "courses"=>array(array("courseName"=>"语文", "score"=>92),array("courseName"=>"数学", "score"=>81))),
array("name"=>"王五", "number"=>"003", "courses"=>array(array("courseName"=>"语文", "score"=>87),array("courseName"=>"数学", "score"=>78)))
);

We need to query the information of all students whose math scores are greater than 90 points, and output the student's name, student ID, and course information, including course name and grades.

First, we can use a foreach loop to traverse each student element in the array $students. After entering the loop, we can then use a foreach loop to traverse the array of courses taken by the student to check whether the math score is greater than 90 points. If If the grades meet the requirements, the student's name, student number, and course information will be output. The code is as follows:

foreach($students as $student){
$courses = $student["courses"];
foreach($courses as $course){
if($course["courseName"]=="数学"){
if($course["score"]>90){
echo "姓名:".$student["name"]." 学号:".$student["number"]." 课程:".$course["courseName"]." 成绩:".$course["score"]." \n";
}
}
}
}

In the above code, we first use a foreach loop to traverse each student element in the array $students, Then use the $courses sub-array to traverse courses and query grades, and output student information that meets the requirements.

4. Summary

This article introduces the method of PHP multi-dimensional array query to output this row, and illustrates it through examples. In actual development, the application scenarios and requirements of multi-dimensional arrays vary widely, and developers need to flexibly apply and process them according to specific situations. I hope this article can provide some reference and help to readers.

The above is the detailed content of How to query multi-dimensional array in php and output this row. 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