Detailed explanation and case analysis of PHP two-dimensional array

PHPz
Release: 2023-04-19 10:39:01
Original
1564 people have browsed it

In PHP, two-dimensional array is a very commonly used data structure type. A two-dimensional array can represent a data table, where each row represents a data record and each column represents a specific data item. This article will explain in detail the concept and usage of PHP two-dimensional arrays, as well as a case analysis combined with actual development.

1. What is a two-dimensional array?

In PHP, array (Array) is a very common basic variable type. Arrays can store multiple values, and these values can be accessed and modified through array subscripts. A two-dimensional array refers to an array that stores multiple arrays, and each nested array represents a row of data. That is, an array contains multiple arrays, and each array contains multiple values. By analogy, we can understand a multidimensional array as an array containing multiple two-dimensional arrays.

The method of declaring a two-dimensional array is very simple. You only need to add a set of square brackets after the array variable name. For example:

$students = array(

array('name'=>'张三', 'score'=>85), array('name'=>'李四', 'score'=>90), array('name'=>'王五', 'score'=>80)
Copy after login

);

2. Use of PHP two-dimensional array

  1. Access two-dimensional array elements

When accessing two-dimensional array elements, you need to specify two subscripts. The first subscript indicates the row, and the second subscript indicates the column. Indexes start counting from 0. For example:

echo $students0; // Output: Zhang San

In the above example, $students[0] represents the first row of the entire table, and ['name'] It is the data in the "Name" column in the first row. Therefore, the code outputs "Zhang San".

  1. Traverse a two-dimensional array

You can use loop statements to traverse a two-dimensional array, for example:

for($i=0;$i

echo $students[$i]['name'].'的成绩是:'.$students[$i]['score'].'
';

}

When using a loop statement to traverse a two-dimensional array, please note:

  • First, we need to traverse the two-dimensional array Each row of the dimensional array (the first subscript);
  • Then, traverse each column (the second subscript) in each row to complete access to each element.

3. Case analysis of PHP two-dimensional array

The following are several PHP two-dimensional array application cases that may be encountered in actual development. Through these cases, we can understand and master the use of PHP two-dimensional arrays more intuitively.

  1. Statistics on everyone’s grades

Suppose there is a two-dimensional array that records class grades. The data is as follows:

$students = array(

array('name'=>'张三', 'score'=>85), array('name'=>'李四', 'score'=>90), array('name'=>'王五', 'score'=>80)
Copy after login

);

We need to count everyone’s results and output them to the web page. This requirement can be achieved by traversing the array:

for($i=0;$i

echo $students[$i]['name'].'的成绩是:'.$students[$i]['score'].'
';

}

Above The output result of the code is:

Zhang San’s grade is: 85
Li Si’s grade is: 90
Wang Wu’s grade is: 80

  1. Calculate class grade Average score

In the first case, we already know how to traverse a two-dimensional array. Calculating the average score of a class can also be achieved by traversing the array:

$total_score = 0;
for($i=0;$i

$total_score += $students[$i]['score'];
Copy after login

}
$avg_score = $total_score / count($students);
echo 'The average score of the class score:'.$avg_score;

The output result of the above code is:

The average score of the class score: 85

  1. Sort by grades from high to low

In the second case, we already know how to calculate the average score of the class. We can use this method to sort by grades from high to low. , the code is as follows:

// First calculate the total score of each student and sort according to the total score
for($i=0;$i

$students[$i]['total_score'] = $students[$i]['score'];
Copy after login

}
for($i=0;$i

for($j=0;$j
         
Copy after login

}

// Output the sorted results
for($i=0;$i

echo $students[$i]['name'].':'.$students[$i]['score'].'分
';

}

The output result of the above code is:

李Four: 90 points
Zhang San: 85 points
Wang Wu: 80 points

Through the above cases, we can see that the use of PHP two-dimensional arrays is very flexible and can easily solve various problems. Practical problems. In the actual development process, we should make more use of PHP two-dimensional arrays and related methods to improve our programming efficiency and code quality.

The above is the detailed content of Detailed explanation and case analysis of PHP two-dimensional array. 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 admin@php.cn
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!