Home > Backend Development > PHP Problem > Can arrays be placed in php arrays?

Can arrays be placed in php arrays?

青灯夜游
Release: 2023-03-16 06:28:01
Original
3032 people have browsed it

Arrays can be placed in php arrays. PHP arrays can store all types of data, including the array itself; if the elements in an array are another array, it forms an array containing the array, that is, a multi-dimensional array. Different dimensions of the array indicate that several subscripts (indexes) are needed to obtain the corresponding array elements. For example, a two-dimensional array requires two subscripts.

Can arrays be placed in php arrays?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Arrays can be placed in php arrays.

Array is one of the most important data types in PHP and is widely used in PHP. Because PHP is a programming language with weak data types, array variables in PHP can store any number of data of any type, and can implement the functions of data structures such as heaps, stacks, and queues in other strong data types.

Arrays in PHP can store all types of data, including the array itself. If the elements in one array are another array, an array containing the array is formed, that is, a multi-dimensional array:

  • Two-dimensional array

  • Three-dimensional array

  • Four-dimensional array

  • ...

But the array exceeds three dimensions Then the readability will be greatly reduced, and it will also be inconvenient to manage.

Let’s take a look at how to define a multi-dimensional array:

1. Define a two-dimensional array

Two-dimensional array and One-dimensional arrays are declared in the same way, except that one or more elements in the array are also declared as an array. There are also two methods of declaring a two-dimensional array, directly assigning values ​​to the array elements and using the array() function.

1), directly assign values ​​to array elements

We can pass the format of "$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:

Can arrays be placed in php arrays?

The row subscript of the two-dimensional array The 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:

Can arrays be placed in php arrays?

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:

Can arrays be placed in php arrays?

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:

Can arrays be placed in php arrays?

2), use the array() function

Using the array() function to declare a two-dimensional array is the same as declaring one Dimensional arrays are similar.

  • 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:

Can arrays be placed in php arrays?

  • 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:

Can arrays be placed in php arrays?

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:

Can arrays be placed in php arrays?

Extension: Get the elements in the two-dimensional array

Just need to indicate each The subscript of a dimension can

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

Can arrays be placed in php arrays?

Note:The different dimensions of the array mark our Several subscripts (indexes) need to be used to obtain the corresponding array elements. For example, a two-dimensional array requires two subscripts to obtain the corresponding array elements, a three-dimensional array requires three, and so on.

2. Creation of multi-dimensional arrays

Refer to the two-dimensional array and draw inferences, you can easily create three-dimensional arrays and four-dimensional arrays Or other higher dimensional arrays. An example of defining a three-dimensional array is as follows:

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

Can arrays be placed in php arrays?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Can arrays be placed in php arrays?. 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