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)
);
2. Use of PHP two-dimensional array
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".
You can use loop statements to traverse a two-dimensional array, for example:
for($i=0;$iecho $students[$i]['name'].'的成绩是:'.$students[$i]['score'].'
';
}
When using a loop statement to traverse a two-dimensional array, please note:
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.
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)
);
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 } The output result of the above code is: The average score of the class score: 85 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 } } // Output the sorted results } The output result of the above code is: 李Four: 90 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!echo $students[$i]['name'].'的成绩是:'.$students[$i]['score'].'<br>';</p>
<p>}</p>
<p>Above The output result of the code is: </p>
<p> Zhang San’s grade is: 85<br> Li Si’s grade is: 90<br> Wang Wu’s grade is: 80</p>
<ol start="2"><li>Calculate class grade Average score</li></ol>
<p>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: </p>
<p>$total_score = 0;<br>for($i=0;$i<count($students);$i ) {</p><pre class="brush:php;toolbar:false">$total_score += $students[$i]['score'];
$avg_score = $total_score / count($students);
echo 'The average score of the class score:'.$avg_score;
for($i=0;$i$students[$i]['total_score'] = $students[$i]['score'];
for($i=0;$ifor($j=0;$j<count($students)-1-$i;$j++) {
if($students[$j]['total_score'] < $students[$j+1]['total_score']) {
$temp = $students[$j+1];
$students[$j+1] = $students[$j];
$students[$j] = $temp;
}
}
for($i=0;$iecho $students[$i]['name'].':'.$students[$i]['score'].'分
';
Zhang San: 85 points
Wang Wu: 80 points