Home  >  Article  >  Backend Development  >  What is the representation method of three-dimensional array in php

What is the representation method of three-dimensional array in php

PHPz
PHPzOriginal
2023-04-19 10:05:431010browse

Arrays in PHP are divided into three forms: one-dimensional array, multi-dimensional array and associative array. Among them, multi-dimensional array means that the array contains an array, that is, the elements of the array are still arrays. In actual development, we often need to use multi-dimensional arrays to represent some complex data structures, such as two-dimensional tables, JSON data, etc. Next, we will introduce the three-dimensional array representation method in PHP in detail.

1. Define a three-dimensional array

The method of defining a three-dimensional array is similar to defining a one-dimensional array and a two-dimensional array. It is also defined using array() or "[]". For example, the following is a three-dimensional array defined using array():

$arr = array(
           array(
                 array("apple", "orange"), 
                 array("banana", "peach")
           ),
           array(
                 array("car", "bike"), 
                 array("bus", "train")
           )
       );

The above code defines a three-dimensional array, in which the first dimension has two elements, and each element is a two-dimensional array. There are two elements in each two-dimensional array, and each element is a one-dimensional array. Finally, there are two elements in each one-dimensional array, representing different fruits, transportation and other information.

2. Three-dimensional array access method

To access elements in a three-dimensional array, you need to specify three index values, which represent the element positions of the first, second, and third dimensions in turn. For example, the following code outputs some elements of the above three-dimensional array:

echo $arr[0][0][0];  //输出apple
echo $arr[1][1][0];  //输出bus
echo $arr[0][1][1];  //输出peach

3. Traversal method of three-dimensional array

The method of traversing a three-dimensional array is the same as traversing a one-dimensional array and a two-dimensional array. different. Below, we introduce two commonly used three-dimensional array traversal methods.

1. Use nested loops to traverse three-dimensional arrays

Using nested loops to traverse three-dimensional arrays is a feasible method. For example, the following code uses a two-level for loop to traverse the three-dimensional array $arr and output the value of each array element:

for ($i=0; $i<count($arr); $i++) {
  for ($j=0; $j<count($arr[$i]); $j++) {
    for ($k=0; $k<count($arr[$i][$j]); $k++) {
      echo $arr[$i][$j][$k]."<br/>";
    }
  }
}

In the above code, the first level of loop traverses the first dimension, and the second level of loop traverses The second dimension and the third layer loop through the third dimension and output the value of each array element in turn.

2. Use recursive functions to traverse three-dimensional arrays

Recursive functions refer to a technique of calling the function itself, which is suitable for traversing multi-dimensional arrays. The following is an example code for a recursive function to traverse a three-dimensional array:

function traverse($arr) {
  foreach ($arr as $key=>$value) {
    if (is_array($value)) {
      traverse($value);
    } else {
      echo $value."<br/>";
    }
  }
}
traverse($arr);

In the above code, a recursive function named traverse() is defined. The function name can be chosen by yourself. The parameter of the function is a two-dimensional array. The function uses a foreach loop to traverse each element in the array. If the current element is an array, it calls itself recursively and continues to traverse the next level of array. If the current element is not an array, the value of the element is output directly.

The above are the two methods of representing three-dimensional arrays and traversing three-dimensional arrays in PHP. Mastering these methods can help us better handle multi-dimensional arrays and improve the efficiency of code development.

The above is the detailed content of What is the representation method of three-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
Previous article:How to escape lt in phpNext article:How to escape lt in php