Home  >  Article  >  Backend Development  >  How to display the contents of a two-dimensional array in php

How to display the contents of a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-24 15:48:42674browse

In PHP development, arrays are a very important data structure. A two-dimensional array refers to an array that contains another array within an array. It is usually used to store multi-dimensional data.

In actual development, we often need to display the contents of a two-dimensional array. Next, we will introduce how to display the contents of a two-dimensional array.

Method 1: Use a loop to traverse a two-dimensional array

Using a loop to traverse a two-dimensional array is the most common method. We can use foreach to traverse a two-dimensional array. The code is as follows:

$arr = array(
    array('name' => '张三','age' => 20),
    array('name' => '李四','age' => 30),
    array('name' => '王五','age' => 40)
);
foreach ($arr as $value) {
    foreach ($value as $key => $data) {
        echo $key . ":" . $data . " ";
    }
    echo "<br />";
}

In the above code, we first define a two-dimensional array containing three elements, and then use two foreach loops to traverse the outer array and inner array of the two-dimensional array respectively, and finally output the key of each element. value pair.

Method 2: Use table tag to display two-dimensional array

If we need to clearly display the two-dimensional array in the form of a table on the page, we can use the table tag in HTML to achieve this , the code is as follows:

$arr = array(
    array('name' => '张三','age' => 20),
    array('name' => '李四','age' => 30),
    array('name' => '王五','age' => 40)
);
echo "<table border=&#39;1&#39;>";
echo "<tr><td>姓名</td><td>年龄</td></tr>";
foreach ($arr as $value) {
    echo "<tr>";
    foreach ($value as $data) {
        echo "<td>$data</td>";
    }
    echo "</tr>";
}
echo "</table>";

In the above code, we use the table tag and related attributes in HTML to define the format of the table, and then use two foreach loops to traverse the elements in the two-dimensional array, and finally output the data table .

Method 3: Use the print_r() function to display the two-dimensional array

In addition to the above two methods, we can also use the print_r() function to directly output the contents of the two-dimensional array. The code is as follows:

$arr = array(
    array('name' => '张三','age' => 20),
    array('name' => '李四','age' => 30),
    array('name' => '王五','age' => 40)
);
print_r($arr);

The print_r() function is a very commonly used output function in PHP. It is used to print the contents of variables in an easy-to-read manner. In the above code, we directly pass the two-dimensional array into the print_r() function for output.

Summary

Two-dimensional arrays are widely used in PHP development. The above three methods can be used to display the contents of two-dimensional arrays. The specific use can be selected according to actual needs. In actual development, we need to master different methods and techniques and make reasonable use of the basic operations of PHP to better complete development tasks.

The above is the detailed content of How to display the contents of a two-dimensional array in php. 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