What does php three-dimensional array mean?

青灯夜游
Release: 2023-03-17 18:52:02
Original
1484 people have browsed it

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....),...),...); ".

What does php three-dimensional array mean?

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[&#39;安徽&#39;][&#39;合肥&#39;][0] = &#39;蜀山区&#39;;
$array[&#39;安徽&#39;][&#39;合肥&#39;][1] = &#39;长丰县&#39;;
$array[&#39;安徽&#39;][&#39;合肥&#39;][2] = &#39;肥东&#39;;

$array[&#39;安徽&#39;][&#39;宿州&#39;][0] = &#39;墉桥区&#39;;
$array[&#39;安徽&#39;][&#39;宿州&#39;][1] = &#39;灵璧县&#39;;
$array[&#39;安徽&#39;][&#39;宿州&#39;][2] = &#39;泗县&#39;;
var_dump($array);
?>
Copy after login

What does php three-dimensional array mean?

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[&#39;安徽&#39;][][0] = &#39;蜀山区&#39;;
$array[&#39;安徽&#39;][][1] = &#39;长丰县&#39;;
$array[&#39;安徽&#39;][][2] = &#39;肥东&#39;;

$array[&#39;安徽&#39;][&#39;宿州&#39;][] = &#39;墉桥区&#39;;
$array[&#39;安徽&#39;][&#39;宿州&#39;][] = &#39;灵璧县&#39;;
$array[&#39;安徽&#39;][&#39;宿州&#39;][] = &#39;泗县&#39;;
var_dump($array);
?>
Copy after login

What does php three-dimensional array mean?

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(
        &#39;安徽&#39; => array(
            &#39;合肥&#39;=>array(&#39;蜀山区&#39;,&#39;长丰县&#39;,&#39;肥东&#39;),
			&#39;宿州&#39;=>array(&#39;墉桥区&#39;,&#39;灵璧县&#39;,&#39;泗县&#39;)
        ),
        &#39;河南&#39; => array(
            &#39;洛阳&#39;=>array(&#39;西工区&#39;,&#39;老城区&#39;,&#39;孟津县&#39;),
            &#39;郑州市&#39;=>array(&#39;中原区&#39;,&#39;金水区&#39;)
        )
);
 var_dump($array);
?>
Copy after login

What does php three-dimensional array mean?

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!