What does multidimensional array in php mean?
The multi-dimensional array in php refers to the meaning of 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 index number of the elements you need to select. , for example, a two-dimensional array requires two indexes to select elements, and a three-dimensional array requires three indexes to select elements.
The operating system of this tutorial: Windows10 system, PHP version 8.1.3, 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
Two-dimensional arrays are arrays of arrays (three-dimensional arrays are arrays of arrays of arrays).
First, let’s take a look at the table below:
Brand Inventory Sales
Volvo 33 20
BMW 17 15
Saab 5 2
Land Rover 15 11
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) );
Now this two-dimensional array contains Four arrays, and it has two indices (subscripts): row and column.
To access an element in the $cars array we must use two indices (row and column):
Example
<?phpheader("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>"; ?>
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>"; } ?>
two-dimensional Array creation method:
The two-dimensional array and one-dimensional array are created in the same way, 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 create and initialize two elements in the format of "$array variable name [row subscript] [column subscript] = value;" Dimensional array
<?php header("Content-type:text/html;charset=utf-8"); $array[0]['姓名'] = '张三'; $array[0]['年龄'] = '25'; $array[0]['性别'] = '男'; $array[1]['姓名'] = '李四'; $array[1]['年龄'] = '21'; $array[1]['性别'] = '男'; $array[2]['姓名'] = '娜娜'; $array[2]['年龄'] = '22'; $array[2]['性别'] = '女'; var_dump($array); ?>
Output result:
The row subscript and column subscript of the two-dimensional array can be empty (that is, no specific index value is specified) , then the default is a numeric index, and the index value starts from 0 and increases in sequence by default.
Example 1: "Column subscript" is empty
<?phpheader("Content-type:text/html;charset=utf-8"); $array[0][] = '张三'; $array[0][] = '25'; $array[0][] = '男'; $array[1][] = '李四'; $array[1][] = '21'; $array[1][] = '男'; $array[2][] = '娜娜'; $array[2][] = '22'; $array[2][] = '女'; var_dump($array); ?>
Output result:
Example 2: "Row subscript" is Empty
<?php header("Content-type:text/html; charset=utf-8"); $array[]['姓名'] = '张三'; $array[]['年龄'] = '25'; $array[]['性别'] = '男'; $array[]['姓名'] = '李四'; $array[]['年龄'] = '21'; $array[]['性别'] = '男'; $array[]['姓名'] = '娜娜'; $array[]['年龄'] = '22'; $array[]['性别'] = '女'; var_dump($array); ?>
Output result:
Example 3: "Row subscript" and "Column subscript" are both empty
<?php header("Content-type:text/html;charset=utf-8"); $array[][] = '张三'; $array[][] = '25'; $array[][] = '男'; $array[][] = '李四'; $array[][] = '21'; $array[][] = '男'; $array[][] = '娜娜'; $array[][] = '22'; $array[][] = '女'; var_dump($array); ?>
Output result:
#2. Use the array() function
Using the array() function to declare a two-dimensional array is similar to declaring a one-dimensional array.
1) Index array
<?php header("Content-type:text/html;charset=utf-8"); $array = array( array("张三",25,"男"), array("李四",21,"男"), array("娜娜",22,"女")); var_dump($array); ?>
Output result:
<?php header("Content-type:text/html;charset=utf-8"); $array = array( "学生1"=>array("张三",25,"男"), "学生2"=>array("李四",21,"男"), "学生3"=>array("娜娜",22,"女")); var_dump($array); ?>
<?php header("Content-type:text/html;charset=utf-8"); $array = array( "学生1"=>array("姓名"=>"张三","年龄"=>25,"性别"=>"男"), "学生2"=>array("姓名"=>"李四","年龄"=>21,"性别"=>"男"), "学生3"=>array("姓名"=>"娜娜","年龄"=>22,"性别"=>"女")); var_dump($array); ?>
<?php header("Content-type:text/html;charset=utf-8"); $array = array( array("姓名"=>"张三","年龄"=>25,"性别"=>"男"), array("姓名"=>"李四","年龄"=>21,"性别"=>"男"), array("姓名"=>"娜娜","年龄"=>22,"性别"=>"女")); echo $array[0]['姓名'].'同学的年龄为'.$array[0]['年龄'].'岁'; ?>
<?php header("Content-type:text/html;charset=utf-8"); $array = array( '安徽' => array( '合肥'=>array('蜀山区','长丰县','肥东'), '宿州'=>array('墉桥区','灵璧县','泗县') ), '河南' => array( '洛阳'=>array('西工区','老城区','孟津县'), '郑州市'=>array('中原区','金水区') ) ); var_dump($array); ?>
The above is the detailed content of What does multidimensional array in php mean?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.
