A detailed explanation of the usage of multi-dimensional arrays in PHP

PHPz
Release: 2023-04-18 14:44:08
Original
545 people have browsed it

In PHP development, arrays are a very important data structure, and multidimensional arrays are a more complex array type. As the size and requirements of projects increase, programmers need to use a variety of array structures to meet data processing needs.

Introduction to multidimensional arrays

A multidimensional array simply refers to an array containing multiple one-dimensional arrays. In PHP, the nature of a multidimensional array is recursive, i.e. it can continue to contain multiple nested multidimensional arrays. Unlike regular arrays, the elements in a multidimensional array can be an array (a single value or a collection of multiple values). You need to specify multiple key names to get or set values ​​in a multidimensional array.

In PHP, using arrays to store data is one of the most commonly used methods. Multidimensional arrays are a way to store more complex structured data. Therefore, proficiency in the use of multi-dimensional arrays is very important for processing complex data structures.

Declaring a multidimensional array

Creating a multidimensional array requires the use of multiple array symbols. For example, the following code will create a 2-dimensional array containing nested 1-dimensional arrays:

$scores = array(
    array("Tom", 98),
    array("Jerry", 89),
    array("Spike", 66)
);
Copy after login

The above code stores 3 arrays in the array $scores. Each array contains 2 elements. The first element is A string variable representing the student's name; the second element is an integer variable representing the student's score.

Reading and writing multi-dimensional arrays

To read multi-dimensional array elements, you only need to specify the complete key name. For example, you can use the following code to get Tom's score:

echo $scores[0][1];
Copy after login

In the above code, $scores[0] points to the first array, and [1] points to the second element, which is Tom's score of 98.

Similarly, you can also traverse the array to obtain all student names and scores in the following way:

foreach ($scores as $score) {
    echo $score[0].": ".$score[1]."<br>";
}
Copy after login

The above code will output the following content in sequence:

Tom: 98
Jerry: 89
Spike: 66
Copy after login

Change the multi-dimensional array Values ​​in other internal arrays also need to specify complete key names. For example, the following code will change Jerry's score:

$scores[1][1] = 95;
Copy after login

In the above code, $scores[1] points to the second array, and [1] points to the second element, which is Jerry's score. The code changes Jerry's score from 89 to 95.

Traversing multi-dimensional arrays

There are many ways to traverse multi-dimensional arrays. The following are two of the most common methods.

1. Use a loop to traverse:

$scores = array(
    array("Tom", 98),
    array("Jerry", 89),
    array("Spike", 66)
);

for ($i = 0; $i < count($scores); $i++) {
    for ($j = 0; $j < count($scores[$i]); $j++) {
        echo $scores[$i][$j]." ";
    }
    echo "
"; }
Copy after login

The above code will output the following content in sequence:

Tom 98
Jerry 89
Spike 66
Copy after login
Copy after login

2. Use foreach to traverse:

$scores = array(
    array("Tom", 98),
    array("Jerry", 89),
    array("Spike", 66)
);

foreach ($scores as $score) {
    foreach ($score as $value) {
        echo $value." ";
    }
    echo "
"; }
Copy after login

Above The code will also output the following content:

Tom 98
Jerry 89
Spike 66
Copy after login
Copy after login

Summary

Multidimensional array is a very powerful tool that can help us implement complex data processing operations. Although it is a little complicated, it is very convenient to use as long as you master its basic usage. Whether it is processing financial data or developing games, multidimensional arrays are a very practical function.

The above is the detailed content of A detailed explanation of the usage of multi-dimensional arrays 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