Home  >  Article  >  Backend Development  >  How to define multidimensional array in php

How to define multidimensional array in php

PHPz
PHPzOriginal
2023-04-26 10:28:49703browse

PHP is a widely used server-side scripting language. It is widely used in the field of Web development because it is easy to learn and use and supports a variety of databases. Arrays in PHP are one of the most commonly used data types. It allows us to store multiple values ​​in a variable. In actual project development, we often encounter the need to store multiple similar data, such as cities, provinces and other data types. In this case, we need to use multi-dimensional arrays in PHP. This article will introduce how to define multi-dimensional arrays in PHP.

1. What is a multidimensional array

A multidimensional array refers to an array that contains other arrays. In other words, a multidimensional array is an "array collection" composed of a series of single arrays.

Let’s use a specific example to illustrate. For example, we want to save the following student information:

Name: Zhang San
Age: 20
Gender: Male
City: Beijing

Name: Li Si
Age: 21
Gender: Male
City: Shanghai

Name: Wang Wu
Age: 22
Gender: Female
City: Guangzhou

We can define a two-dimensional array$students, where each element is a one-dimensional array to store the information of each student .

2. Define a two-dimensional array

In PHP, we can define a two-dimensional array in the following ways:

// 方法一
$array = array(
    array(1, 2, 3),
    array("name"=>"Tom", "age"=>30),
    array("name"=>"Jerry", "age"=>25),
);

// 方法二
$array[0] = array(1, 2, 3);
$array[1] = array("name"=>"Tom", "age"=>30);
$array[2] = array("name"=>"Jerry", "age"=>25);

In method one, we used ## directly #array() function, with multiple one-dimensional arrays nested in it. In method two, we first define a one-dimensional array, and then instantiate it into two-dimensional arrays one by one.

3. Define a three-dimensional array

If you want to define a three-dimensional array, the method is similar to defining a two-dimensional array. Taking a student grade list as an example, we can define the following three-dimensional array:

$grades = array(
    array(
        array("math"=>90, "english"=>85, "science"=>95),
        array("math"=>85, "english"=>80, "science"=>92),
    ),
    array(
        array("math"=>95, "english"=>92, "science"=>80),
        array("math"=>88, "english"=>90, "science"=>87),
    ),
);
Each element in this array is a two-dimensional array composed of multiple one-dimensional arrays containing subjects and grades. . And each two-dimensional array is a student's transcript.

4. Loop through multi-dimensional arrays

To traverse multi-dimensional arrays, you can use nested

for, foreach, while etc. statements.

Taking a two-dimensional array as an example, we can use the

foreach statement to traverse:

foreach ($students as $student) {
    echo "姓名:". $student['name']. "<br>";
    echo "年龄:". $student['age']. "<br>";
    echo "性别:". $student['gender']. "<br>";
    echo "城市:". $student['city']. "<br>";
    echo "<hr>";
}
For a three-dimensional array, a multi-level loop must be used:

foreach ($grades as $grade) {
    foreach ($grade as $subject) {
        echo "数学成绩:". $subject['math']. "<br>";
        echo "英语成绩:". $subject['english']. "<br>";
        echo "科学成绩:". $subject['science']. "<br>";
        echo "<hr>";
    }
}
5. Summary

This article introduces the definition and traversal method of multi-dimensional arrays in PHP. In practice, we often need to use multi-dimensional arrays to store and process data. Therefore, mastering the definition and basic operations of multi-dimensional arrays will help improve efficiency and quality in projects.

The above is the detailed content of How to define multidimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

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