Home > Backend Development > PHP Problem > How to change elements under multidimensional array in php

How to change elements under multidimensional array in php

PHPz
Release: 2023-04-27 10:28:53
Original
613 people have browsed it

PHP is a very popular web programming language that is popular because of its wide range of applications. Arrays are a very useful data type in PHP. There are many types of arrays in PHP, the most commonly used of which are multi-dimensional arrays. In a multidimensional array, each array element can be an array, forming a two-dimensional, three-dimensional or higher-dimensional array structure. However, when dealing with multidimensional arrays, modifying specific elements can be a bit tricky. This article explains how to change elements in a multidimensional array.

Traversal of multidimensional arrays

Before understanding how to change multidimensional array elements, we need to understand how to traverse multidimensional arrays in PHP. There are many ways to traverse multi-dimensional arrays, below we will introduce the three most common methods.

Method 1: Use for loop to traverse

Using for loop can easily traverse a two-dimensional array. For three-dimensional, four-dimensional or higher-dimensional arrays, multiple for loops need to be nested. As shown below:

$array = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        echo $array[$i][$j] . " ";
    }
    echo "<br>";
}
Copy after login

The output result is:

1 2 3 
4 5 6 
7 8 9
Copy after login
Copy after login
Copy after login

Method 2: Use foreach loop to traverse

Use foreach loop to traverse arrays of any dimension, as shown below:

$array = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

foreach ($array as $value1) {
    foreach ($value1 as $value2) {
        echo $value2 . " ";
    }
    echo "<br>";
}
Copy after login

The output result is:

1 2 3 
4 5 6 
7 8 9
Copy after login
Copy after login
Copy after login

Method 3: Use recursive traversal

Use the recursive method to traverse arrays of any dimension, as shown below:

$array = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

function traverse($array) {
    foreach ($array as $value) {
        if (is_array($value)) {
            traverse($value);
        } else {
            echo $value . " ";
        }
    }
    echo "<br>";
}

traverse($array);
Copy after login

The output result is the same as the first two methods:

1 2 3 
4 5 6 
7 8 9
Copy after login
Copy after login
Copy after login

Modify the elements of a multi-dimensional array

After you know how to traverse a multi-dimensional array, it is easier to modify the elements of the array. In PHP, we can use the subscript of an array to modify the value of an element. Below we will introduce the methods of modifying elements in two-dimensional arrays and three-dimensional arrays respectively.

Modifying elements under a two-dimensional array

It is very simple to modify elements under a two-dimensional array. We just need to use the subscript of the array to update the value of the element as follows:

$array = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

$array[1][1] = 10;

for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        echo $array[$i][$j] . " ";
    }
    echo "<br>";
}
Copy after login

The output is:

1 2 3 
4 10 6 
7 8 9
Copy after login

In the above example, we use $array[ 1][1] = 10; Updates the elements in the two-dimensional array. We then use a for loop to verify that the element has been updated.

Modifying elements under a three-dimensional array

Modifying elements under a three-dimensional array is a little more complicated. We need to use multiple array subscripts to access elements in the array. As shown below:

$array = array(
    array(
        array(1, 2, 3),
        array(4, 5, 6),
        array(7, 8, 9)
    ),
    array(
        array(10, 11, 12),
        array(13, 14, 15),
        array(16, 17, 18)
    )
);

$array[1][1][1] = 20;

function traverse($array) {
    foreach ($array as $value) {
        if (is_array($value)) {
            traverse($value);
        } else {
            echo $value . " ";
        }
    }
    echo "<br>";
}

traverse($array);
Copy after login

The output is:

1 2 3 
4 5 6 
7 8 9 
10 11 12 
13 20 15 
16 17 18
Copy after login

In the above example, we use $array[1][1][1] = 20; Updated elements in a three-dimensional array. Since recursion is required when traversing a three-dimensional array, we use the traverse function to traverse the entire array.

Summary

In this article, we introduced how to modify elements in a multidimensional array using PHP. We learned about three ways to work with multidimensional arrays and provided examples of two- and three-dimensional arrays that demonstrate how to use array subscripts to access and update array elements. In this way, everyone can use PHP more flexibly to process complex multi-dimensional array structures.

The above is the detailed content of How to change elements under multidimensional 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