What is a php multidimensional array?

青灯夜游
Release: 2023-03-17 19:00:02
Original
1146 people have browsed it

In PHP, a multi-dimensional array refers to an array containing one or more arrays. For example, a two-dimensional array is a one-dimensional array with multiple one-dimensional arrays defined inside it. The dimensions of the array indicate the number of indexes you need to select elements. For example, a two-dimensional array requires two indexes to select an element, and a three-dimensional array requires three indexes to select an element.

What is a php multidimensional array?

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

PHP - Multidimensional Array

Multidimensional array refers to an array containing one or more arrays.

PHP can understand multi-dimensional arrays of two, three, four or five levels or even more levels. However, arrays more than three levels deep are difficult for most people to manage.

Note: The dimensions of the array indicate the index number of elements you need to select.

  • For a two-dimensional array, you need two indexes to select elements

  • For a three-dimensional array, you need three indexes to select an element

PHP - Two-dimensional array

A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays) ).

First, let’s take a look at the table below:

##Land Rover1511

We can store the data in the above table in a two-dimensional array, like this:

$cars = array
  (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
  );
Copy after login

Now this two-dimensional array contains four arrays, and it has two indexes (subscripts): row and column.

To access an element in the $cars array we must use two indices (row and column):

Example

<?php
header("Content-type:text/html;charset=utf-8");
$cars = array
   (
   array("Volvo",33,20),
   array("BMW",17,15),
   array("Saab",5,2),
   array("Land Rover",15,11)
   );
   
echo $cars[0][0].": 库存:".$cars[0][1].", 已售:".$cars[0][2].".<br>";
echo $cars[1][0].": 库存:".$cars[1][1].", 已售:".$cars[1][2].".<br>";
echo $cars[2][0].": 库存:".$cars[2][1].", 已售:".$cars[2][2].".<br>";
echo $cars[3][0].": 库存:".$cars[3][1].", 已售:".$cars[3][2].".<br>";
?>
Copy after login

What is a php multidimensional array?

We can also use another For loop inside the For loop to get the elements in the $cars array (we still need to use two indexes):

<?php
header("Content-type:text/html;charset=utf-8");
$cars = array
   (
   array("Volvo",33,20),
   array("BMW",17,15),
   array("Saab",5,2),
   array("Land Rover",15,11)
   );
   
for ($row = 0; $row <  4; $row++) {
   echo "<p><b>行数 $row</b></p>";
   echo "<ul>";
   for ($col = 0; $col <  3; $col++) {
     echo "<li>".$cars[$row][$col]."</li>";
   }
   echo "</ul>";
}
?>
Copy after login

What is a php multidimensional array?

How to create a two-dimensional array:

The creation method of a two-dimensional array and a one-dimensional array is the same, just replace the elements in the array with Just 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:

What is a php multidimensional array?

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:

What is a php multidimensional array?

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:

What is a php multidimensional array?

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:

What is a php multidimensional array?

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:

What is a php multidimensional array?

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:

What is a php multidimensional array?

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:

What is a php multidimensional array?

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:

What is a php multidimensional array?

Recommended learning: "PHP Video Tutorial"

Brand Inventory Sales
Volvo 33 20
BMW 17 15
Saab 5 2

The above is the detailed content of What is a php multidimensional array?. 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