array(key name=>array(key name=>key value, key name=>key value....),...),...);"."/> array(key name=>array(key name=>key value, key name=>key value....),...),...);".">
Home > Article > Backend Development > What does php three-dimensional array mean?
In PHP, a three-dimensional array refers to an array structure with three dimensions, that is, the elements in the main array are one or more arrays, and the elements in the sub-array are also one or more arrays. Methods to define a three-dimensional array: 1. Directly assign values to the array elements with the syntax "$array variable name [one-dimensional subscript] [two-dimensional subscript] [three-dimensional subscript] = value;"; 2. Use the array() function to define , the syntax "array(key name=>array(key name=>array(key name=>key value, key name=>key value....),...),...); ".
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
In PHP, a multi-dimensional array contains one or more An array of arrays, where a three-dimensional array refers to an array structure with three dimensions.
In a three-dimensional array, the elements in the main array are one or more arrays, and the elements in the subarray are also one or more arrays.
Three-dimensional arrays are created in the same way as one-dimensional arrays and two-dimensional arrays, just replace the elements in the array with arrays. There are also two methods: "directly assigning values to array elements" and "array() function". Below we will introduce these two methods in detail with our actual code examples.
1. Directly assign values to array elements
We can pass the format of "$array variable name [one-dimensional subscript] [two-dimensional subscript] [Three-dimensional subscript]=value;
" format to create and initialize the three-dimensional array
<?php header("Content-type:text/html;charset=utf-8"); $array['安徽']['合肥'][0] = '蜀山区'; $array['安徽']['合肥'][1] = '长丰县'; $array['安徽']['合肥'][2] = '肥东'; $array['安徽']['宿州'][0] = '墉桥区'; $array['安徽']['宿州'][1] = '灵璧县'; $array['安徽']['宿州'][2] = '泗县'; var_dump($array); ?>
One-dimensional subscript, two-dimensional subscript and three-dimensional subscript of the three-dimensional array The subscript can be empty (that is, no specific index value is specified), then the default is a numeric index, and the index value increases sequentially starting from 0 by default.
<?php header("Content-type:text/html;charset=utf-8"); $array['安徽'][][0] = '蜀山区'; $array['安徽'][][1] = '长丰县'; $array['安徽'][][2] = '肥东'; $array['安徽']['宿州'][] = '墉桥区'; $array['安徽']['宿州'][] = '灵璧县'; $array['安徽']['宿州'][] = '泗县'; var_dump($array); ?>
2. Use the array() function
Use the array() function to declare a three-dimensional array and a two-dimensional array. Dimensional arrays are similar.
<?php header("Content-type:text/html;charset=utf-8"); $array = array( '安徽' => array( '合肥'=>array('蜀山区','长丰县','肥东'), '宿州'=>array('墉桥区','灵璧县','泗县') ), '河南' => array( '洛阳'=>array('西工区','老城区','孟津县'), '郑州市'=>array('中原区','金水区') ) ); var_dump($array); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does php three-dimensional array mean?. For more information, please follow other related articles on the PHP Chinese website!