Home  >  Article  >  Backend Development  >  How to modify the values ​​in a two-dimensional array in php

How to modify the values ​​in a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-26 10:36:13811browse

In PHP, modifying a value in a two-dimensional array may be a very common operation, because the two-dimensional array is one of the commonly used data structures in PHP development.

There are many ways to modify the value of a two-dimensional array, but the most common is by accessing the keys in the array. Below we will introduce several methods of modifying two-dimensional array values.

  1. By using a foreach loop

You can use a foreach loop to traverse the entire array and modify the values ​​we need to change. The following is a sample code:

$array = array(
    array("name" => "Tom", "age" => 20),
    array("name" => "Jack", "age" => 30),
    array("name" => "Mary", "age" => 25)
);

foreach ($array as $key => $value) {
    if ($value["name"] == "Jack") {
        $array[$key]["age"] = 35;
    }
}

print_r($array);

This code will traverse the entire array, find the entry with a name equal to "Jack", and modify its age to 35 years old. Finally, output the contents of the modified array.

  1. Modify by specifying the key name

You can also modify the values ​​in the two-dimensional array by specifying the key name of the array. The following is an example:

$array = array(
    array("name" => "Tom", "age" => 20),
    array("name" => "Jack", "age" => 30),
    array("name" => "Mary", "age" => 25)
);

$array[1]["age"] = 35;

print_r($array);

This code will find the entry with index 1 and modify its corresponding age to 35 years old. Finally, output the contents of the modified array.

  1. Using the array_map function

Another way to modify the values ​​of a two-dimensional array is to use the array_map function. The following is an example:

$array = array(
    array("name" => "Tom", "age" => 20),
    array("name" => "Jack", "age" => 30),
    array("name" => "Mary", "age" => 25)
);

$newArray = array_map(function($item) {
    if ($item["name"] == "Jack") {
        $item["age"] = 35;
    }
    return $item;
}, $array);

print_r($newArray);

In this example, we use the array_map function to traverse the entire array, and then decide whether to modify its age to 35 by checking the name of the current entry.

No matter which method you take, modifying the value of a two-dimensional array is very simple. The method of using the foreach loop and the array_map function can achieve batch modification, while the method of specifying the key name modification is more suitable for the update of a single value. In actual development, different methods can be selected according to different needs.

The above is the detailed content of How to modify the values ​​in a two-dimensional 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