Home >Backend Development >PHP Problem >How to traverse a two-dimensional array in php

How to traverse a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-23 10:07:192233browse

PHP is a popular server-side scripting language that is often used in website development and application development. In PHP applications, two-dimensional arrays are often used because they can save complex data structures, such as tables, and are also very useful for large-scale data processing.

Traversing a two-dimensional array is a relatively basic operation. This article will introduce several methods of traversing a two-dimensional array in PHP.

1. Use for loop

Using for loop is the most basic way to traverse an array. It traverses a two-dimensional array through two layers of for loops.

$array = array(
     array("John", "Smith", 31),
     array("Jane", "Doe", 23),
     array("Mike", "Jones", 26)
);

for ($row = 0; $row < 3; $row++) {
      echo "<p><b>Row number $row</b></p>";
      echo "<ul>";
      for ($col = 0; $col < 3; $col++) {
           echo "<li>".$array[$row][$col]."</li>";
      }
      echo "</ul>";
}

The above code will output the following content:

Row number 0
- John
- Smith
- 31

Row number 1
- Jane
- Doe
- 23

Row number 2
- Mike
- Jones
- 26

Through the for loop, we can output each element according to the subscript of the two-dimensional array.

2. Use foreach loop

The foreach loop is a more advanced way to traverse an array. It will automatically assign each element in the array to a variable to facilitate reading data.

$array = array(
     array("John", "Smith", 31),
     array("Jane", "Doe", 23),
     array("Mike", "Jones", 26)
);

foreach ($array as $row) {
      echo "<p><b>Row</b></p>";
      echo "<ul>";
      foreach ($row as $value) {
           echo "<li>".$value."</li>";
      }
      echo "</ul>";
}

The above code will output the following:

Row
- John
- Smith
- 31

Row
- Jane
- Doe
- 23

Row
- Mike
- Jones
- 26

As you can see, it is easier to read each element in the array using a foreach loop, and the code is more concise.

3. Use the array_map function

The array_map function can operate on arrays, including traversing two-dimensional arrays. We can use the array_map function to iterate through each element of a two-dimensional array and output them to HTML.

$array = array(
     array("John", "Smith", 31),
     array("Jane", "Doe", 23),
     array("Mike", "Jones", 26)
);

function printRow($row) {
      echo "<p><b>Row</b></p>";
      echo "<ul>";
      array_map(function($value) {
            echo "<li>".$value."</li>";
      }, $row);
      echo "</ul>";
}

array_map("printRow", $array);

The above code will output the following:

Row
- John
- Smith
- 31

Row
- Jane
- Doe
- 23

Row
- Mike
- Jones
- 26

4. Using the array_walk function

The array_walk function allows us to execute a callback function to traverse the array. We can use this to iterate over 2D arrays and output them to HTML.

$array = array(
     array("John", "Smith", 31),
     array("Jane", "Doe", 23),
     array("Mike", "Jones", 26)
);

function printRow($row) {
      echo "<p><b>Row</b></p>";
      echo "<ul>";
      array_walk($row, function($value) {
            echo "<li>".$value."</li>";
      });
      echo "</ul>";
}

array_walk($array, "printRow");

The above code will output the following:

Row
- John
- Smith
- 31

Row
- Jane
- Doe
- 23

Row
- Mike
- Jones
- 26

Using the array_walk function to traverse a two-dimensional array is more concise, and there is no need to use loops in the code.

Summary

Traversing a two-dimensional array is a common operation in PHP development. It is very useful for us to deal with complex data structures. This article introduces several methods of traversing two-dimensional arrays, including for loop, foreach loop, array_map function and array_walk function. You can choose a suitable method to operate two-dimensional arrays according to your needs.

The above is the detailed content of How to traverse 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