In web development, tables are a common form of data display. As a language widely used in Web development, PHP's two-dimensional array provides a convenient way to create and display tables. This article will introduce how to use PHP's two-dimensional array to implement table printing.
1. What is a two-dimensional array in PHP
In PHP, array is a very important data type. Simply put, an array is a collection of data of the same type. The two-dimensional array in PHP is a way of applying an array within an array, that is, the elements in the array are still arrays. For example:
$grades = array( array("Alice", 95, 90, 100), array("Bob", 80, 85, 90), array("Charlie", 75, 70, 80) );
In the above code, $grades is a two-dimensional array containing three elements, and each element is an array. The elements in the array can be accessed and manipulated in various ways. For example, in the above array, you can use $grades0 to get the first value "Alice" of the first element, and use $grades1 to get the third value "90" of the second element. ".
2. Create a simple table
After understanding the basic concepts of PHP two-dimensional arrays, let us implement a simple table. This table will display the results of three students, including their names, ages, Chinese and English scores.
First, we need to create a two-dimensional array containing all the data. For example:
$students = array( array("name" => "Alice", "age" => 18, "Chinese" => 95, "English" => 90), array("name" => "Bob", "age" => 19, "Chinese" => 80, "English" => 85), array("name" => "Charlie", "age" => 20, "Chinese" => 75, "English" => 70) );
In the above code, we use the associative array method to create a two-dimensional array. Among them, each element is an associative array containing name, age, Chinese and English scores. Next, we can use HTML and PHP loop statements to generate tables:
<table> <tr> <th>姓名</th> <th>年龄</th> <th>语文成绩</th> <th>英语成绩</th> </tr> <?php foreach($students as $student): ?> <tr> <td><?php echo $student["name"]; ?></td> <td><?php echo $student["age"]; ?></td> <td><?php echo $student["Chinese"]; ?></td> <td><?php echo $student["English"]; ?></td> </tr> <?php endforeach; ?> </table>
In the above code, we use HTML table tags to create tables. Through loop statements, we can traverse each element in the two-dimensional array and generate the corresponding table rows. Among them, PHP's echo statement is used to output the value of each element.
3. Beautify the table style
If it is just a simple table printing, generally speaking the above code is enough. But sometimes we need to beautify the table style to make the table more beautiful.
First of all, we can add CSS styles to change the table border line and other styles:
table { border-collapse: collapse; width: 100%; margin: auto; } th,td { border: 1px solid #ddd; padding: 8px; text-align: center; } th { background-color: #f2f2f2; color: #333; }
In the above code, we use the border-collapse attribute to merge the table borders into one line, using The padding property is used to set the distance within the cell, and the text-align property is used to set the alignment of the text within the cell, etc. At the same time, we also set the background color and color of the table header and cells to make the table more beautiful.
4. Summary
Through the introduction of this article, we have learned how to use PHP's two-dimensional array to implement table printing. Specifically, we created a two-dimensional array containing all the data in PHP, and then used HTML and PHP loop statements to generate the table. At the same time, we can also use CSS styles to beautify the style of the table.
Of course, form printing can also be achieved in other ways. For example, we can use third-party libraries or front-end frameworks to generate tables. But no matter which method is used, it is necessary to understand and master PHP's two-dimensional array.
The above is the detailed content of PHP two-dimensional array realizes table printing. For more information, please follow other related articles on the PHP Chinese website!