Home > Backend Development > PHP Tutorial > A brief analysis of two-dimensional array creation methods for PHP array learning

A brief analysis of two-dimensional array creation methods for PHP array learning

青灯夜游
Release: 2023-04-10 12:12:01
Original
5986 people have browsed it

PHP supports one-dimensional and multi-dimensional arrays. In the previous article "PHP Array Learning How to Create and Initialize a One-Dimensional Array (Detailed Code Explanation)" we gave you a detailed introduction to one-dimensional arrays through code examples There are several methods of creating and initializing arrays, so let’s take a look at two-dimensional arrays (multi-dimensional arrays) in PHP and see how to create two-dimensional arrays (multi-dimensional arrays). Let’s learn together!

In this article, we will first introduce the creation method of two-dimensional array through code examples, and then expand on the creation method of multi-dimensional array. Let’s take a look below.

The so-called two-dimensional array, simply put, is to define multiple one-dimensional arrays inside a one-dimensional array, that is, the value in one array can be another array (it may be a bit one-sided, but the two-dimensional array The principle is roughly like this). A two-dimensional array is essentially an array with an array as an array element, that is, an "array of arrays", and the type specifier is "array name[constant expression][constant expression]". (Attachment: PHP function array array function video explanation)

1. Creation of two-dimensional array

2 The creation method of dimensional array and one-dimensional array is the same, just replace the elements in the array with an array. 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 use the form "$array variable name[row subscript][column subscript]= value ;” format to create and initialize the two-dimensional array

<?php
header("Content-type:text/html;charset=utf-8");
$array[0][&#39;姓名&#39;] = &#39;张三&#39;;
$array[0][&#39;年龄&#39;] = &#39;25&#39;;
$array[0][&#39;性别&#39;] = &#39;男&#39;;
$array[1][&#39;姓名&#39;] = &#39;李四&#39;;
$array[1][&#39;年龄&#39;] = &#39;21&#39;;
$array[1][&#39;性别&#39;] = &#39;男&#39;;
$array[2][&#39;姓名&#39;] = &#39;娜娜&#39;;
$array[2][&#39;年龄&#39;] = &#39;22&#39;;
$array[2][&#39;性别&#39;] = &#39;女&#39;;
var_dump($array);
?>
Copy after login

Output result:

A brief analysis of two-dimensional array creation methods for PHP array learning

Row subscript of the two-dimensional array and column subscripts 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.

Example 1: "Column subscript" is empty

<?php
header("Content-type:text/html;charset=utf-8");
$array[0][] = &#39;张三&#39;;
$array[0][] = &#39;25&#39;;
$array[0][] = &#39;男&#39;;
$array[1][] = &#39;李四&#39;;
$array[1][] = &#39;21&#39;;
$array[1][] = &#39;男&#39;;
$array[2][] = &#39;娜娜&#39;;
$array[2][] = &#39;22&#39;;
$array[2][] = &#39;女&#39;;
var_dump($array);
?>
Copy after login

Output result:

A brief analysis of two-dimensional array creation methods for PHP array learning

Example 2: "Row subscript" is Empty

<?php
header("Content-type:text/html;charset=utf-8");
$array[][&#39;姓名&#39;] = &#39;张三&#39;;
$array[][&#39;年龄&#39;] = &#39;25&#39;;
$array[][&#39;性别&#39;] = &#39;男&#39;;
$array[][&#39;姓名&#39;] = &#39;李四&#39;;
$array[][&#39;年龄&#39;] = &#39;21&#39;;
$array[][&#39;性别&#39;] = &#39;男&#39;;
$array[][&#39;姓名&#39;] = &#39;娜娜&#39;;
$array[][&#39;年龄&#39;] = &#39;22&#39;;
$array[][&#39;性别&#39;] = &#39;女&#39;;
var_dump($array);
?>
Copy after login

Output result:

A brief analysis of two-dimensional array creation methods for PHP array learning

Example 3: "Row subscript" and "Column subscript" are both empty

<?php
header("Content-type:text/html;charset=utf-8");
$array[][] = &#39;张三&#39;;
$array[][] = &#39;25&#39;;
$array[][] = &#39;男&#39;;
$array[][] = &#39;李四&#39;;
$array[][] = &#39;21&#39;;
$array[][] = &#39;男&#39;;
$array[][] = &#39;娜娜&#39;;
$array[][] = &#39;22&#39;;
$array[][] = &#39;女&#39;;
var_dump($array);
?>
Copy after login

Output result:

A brief analysis of two-dimensional array creation methods for PHP array learning

2. Use the array() function

Use the array() function to declare a two-dimensional array and declare a one-dimensional array Arrays are similar.

1) Index array

<?php
header("Content-type:text/html;charset=utf-8");
$array = array
(
    array("张三",25,"男"),
    array("李四",21,"男"),
    array("娜娜",22,"女")
);
var_dump($array);
?>
Copy after login

Output result:

A brief analysis of two-dimensional array creation methods for PHP array learning

2) Associative array

Example 1 :

<?php
header("Content-type:text/html;charset=utf-8");
$array = array
(
    "学生1"=>array("张三",25,"男"),
    "学生2"=>array("李四",21,"男"),
    "学生3"=>array("娜娜",22,"女")
);
var_dump($array);
?>
Copy after login

Output result:

A brief analysis of two-dimensional array creation methods for PHP array learning

Example 2:

<?php
header("Content-type:text/html;charset=utf-8");
$array = array
(
    "学生1"=>array("姓名"=>"张三","年龄"=>25,"性别"=>"男"),
    "学生2"=>array("姓名"=>"李四","年龄"=>21,"性别"=>"男"),
    "学生3"=>array("姓名"=>"娜娜","年龄"=>22,"性别"=>"女")
);
 var_dump($array);
?>
Copy after login

Output result:

A brief analysis of two-dimensional array creation methods for PHP array learning

Instructions: Get the elements in the two-dimensional array

Just need to indicate the subscript of each dimension

<?php
header("Content-type:text/html;charset=utf-8");
$array = array
(
    array("姓名"=>"张三","年龄"=>25,"性别"=>"男"),
    array("姓名"=>"李四","年龄"=>21,"性别"=>"男"),
    array("姓名"=>"娜娜","年龄"=>22,"性别"=>"女")
);
 echo $array[0][&#39;姓名&#39;].&#39;同学的年龄为&#39;.$array[0][&#39;年龄&#39;].&#39;岁&#39;;
?>
Copy after login

Output result:

张三同学的年龄为25岁
Copy after login

2. Creation of multi-dimensional array

A multi-dimensional array is an array containing one or more arrays.

In a multi-dimensional array, each element in the main array can also be an array, and each element in the sub-array can also be an array.

With reference to the two-dimensional array, we can easily understand the creation method of the multi-dimensional array

Example: Creation of the three-dimensional array

<?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

Output result:

A brief analysis of two-dimensional array creation methods for PHP array learning

Recommended: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of A brief analysis of two-dimensional array creation methods for PHP array learning. For more information, please follow other related articles on the PHP Chinese website!

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