Home Backend Development PHP Problem PHP two-dimensional array realizes table printing

PHP two-dimensional array realizes table printing

May 23, 2023 am 09:18 AM

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)
);
Copy after login

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)
);
Copy after login

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>
Copy after login

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;
}
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

How do you retrieve data from a database using PHP? How do you retrieve data from a database using PHP? Mar 20, 2025 pm 04:57 PM

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

PHP CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

What is the purpose of prepared statements in PHP? What is the purpose of prepared statements in PHP? Mar 20, 2025 pm 04:47 PM

Prepared statements in PHP enhance database security and efficiency by preventing SQL injection and improving query performance through compilation and reuse.Character count: 159

See all articles