How to add a three-dimensional array in PHP

PHPz
Release: 2023-04-25 13:51:11
Original
585 people have browsed it

PHP is a widely used scripting language commonly used in the world of web development. In PHP, an array is a commonly used data type used to store multiple values. Three-dimensional arrays are a special array type that are very useful when dealing with certain complex data structures. This article will introduce how to add a three-dimensional array in PHP.

1. What is a three-dimensional array

In PHP, an array can have multiple dimensions. One-dimensional arrays are the simplest arrays, with only one row of elements. A two-dimensional array is composed of rows of one-dimensional arrays, which can be imagined as a table. The three-dimensional array is more complicated. It can be understood as a list of tables, that is, each table is an element, and each element has multiple values.

We can use a simple example to understand three-dimensional arrays. Suppose we have a class with three students. Each student's information includes name, Chinese score, math score, and English score. We can use a three-dimensional array to store the information of this class, where the first dimension represents each student, the second dimension represents the attributes of each student, and the third dimension represents the value of each attribute:

$class = array(
    array(
        "name" => "张三",
        "score" => array(
            "语文" => 90,
            "数学" => 80,
            "英语" => 70
        )
    ),
    array(
        "name" => "李四",
        "score" => array(
            "语文" => 85,
            "数学" => 75,
            "英语" => 65
        )
    ),
    array(
        "name" => "王五",
        "score" => array(
            "语文" => 95,
            "数学" => 85,
            "英语" => 75
        )
    )
);
Copy after login

2. How to add a three-dimensional array

Adding a three-dimensional array in PHP is more flexible and can be achieved in a variety of ways. Two commonly used methods are introduced below.

  1. Direct assignment

The simplest way is to assign values ​​directly to the three-dimensional array. For example, suppose we want to add a student named "Zhao Liu" to the $class array defined above. His grades are: 80 in Chinese, 90 in mathematics, and 95 in English. You can write it like this:

$class[] = array(
    "name" => "赵六",
    "score" => array(
        "语文" => 80,
        "数学" => 90,
        "英语" => 95
    )
);
Copy after login

This adds an element to the $class array, which is the new student information. You can use the print_r() function to output the $class array to view the results.

  1. Using the array_push() function

Another way is to use the array_push() function. This function adds one or more elements to the end of the array. For three-dimensional arrays, we need to create a new two-dimensional array first, and then add the array to the original three-dimensional array. For example, suppose we want to add a student named "Zhou Qi" to the $class array defined above. His grades are: 85 in Chinese, 95 in mathematics, and 90 in English. You can write it like this:

$new_student = array(
    "name" => "周七",
    "score" => array(
        "语文" => 85,
        "数学" => 95,
        "英语" => 90
    )
);
array_push($class, $new_student);
Copy after login

This adds an element to the $class array, which is the new student information. You can also use the print_r() function to output the $class array to view the results.

3. Summary

Three-dimensional array is a commonly used data structure in PHP and can be used to store complex data information. In PHP, there are many ways to add a three-dimensional array, which can be achieved by direct assignment or using the array_push() function. No matter which method is used, you need to choose the appropriate method based on actual needs.

The above is the detailed content of How to add a three-dimensional array in PHP. 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