Home > Backend Development > PHP Problem > How to change the value of a multidimensional array in PHP

How to change the value of a multidimensional array in PHP

PHPz
Release: 2023-04-27 14:29:27
Original
625 people have browsed it

In PHP development, processing multi-dimensional arrays is a very common operation. Sometimes, we need to modify a multi-dimensional array, for example, we need to change the value of a certain sub-array to another value, or delete a key-value pair, etc. So in PHP, how to change the value of a multidimensional array?

Next, this article will introduce in detail how to use the foreach statement to change the value of a multi-dimensional array.

  1. Basic usage of the foreach statement

In PHP, the foreach statement can be used to traverse the array. The basic syntax of the foreach statement is as follows:

foreach ($array as $key => $value) {
    // 对$value进行操作
}
Copy after login

Among them, $array is the array to be traversed, $key is the key of the currently traversed element in the array, and $value is the value of the currently traversed element in the array. Through these three parameters, we can access and operate the elements in the array.

For example, the following code uses the foreach statement to traverse a simple array:

$fruits = array("apple", "orange", "banana");

foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}
Copy after login

The output of the above code is:

apple
orange
banana
Copy after login
  1. Change the value of a multi-dimensional array

In PHP, to change the value of a multidimensional array, you need to use a nested foreach statement. The following is an example of a two-dimensional array:

$students = array(
    array("name" => "张三", "age" => 18, "score" => 80),
    array("name" => "李四", "age" => 19, "score" => 90),
    array("name" => "王五", "age" => 20, "score" => 85)
);
Copy after login

If we need to change John Doe's score from 90 to 95, we can use the following code:

foreach ($students as &$student) {
    if ($student["name"] == "李四") {
        $student["score"] = 95;
    }
}
Copy after login

Through the above code, we use a The foreach statement traverses the $students array and takes out each subarray $student. In each subarray, determine whether $name is equal to "李思", and if so, change $score to 95. It should be noted that since the foreach statement creates a copy of the subarray, reference & must be used when modifying the subarray.

Next, let’s look at a more complex multi-dimensional array example:

$employees = array(
    array("name" => "张三", "age" => 25, "department" => "技术部"),
    array("name" => "李四", "age" => 26, "department" => "市场部"),
    array("name" => "王五", "age" => 28, "department" => "财务部")
);

$departments = array(
    "技术部" => array("leader" => "赵六", "location" => "办公室1"),
    "市场部" => array("leader" => "钱七", "location" => "办公室2"),
    "财务部" => array("leader" => "孙八", "location" => "办公室3")
);
Copy after login

If we need to change Zhang San’s department from “Technical Department” to “Finance Department”, we can use The following code:

foreach ($employees as &$employee) {
    if ($employee["name"] == "张三") {
        $department = $departments[$employee["department"]];
        $employee["department"] = "财务部";
        $employee["leader"] = $department["leader"];
        $employee["location"] = $department["location"];
    }
}
Copy after login

In the above code, we first use a foreach statement to traverse the $employees array and find the employee whose $name is equal to "Zhang San". Then, we use $employee["department"] as the key to retrieve Zhang San's department from the $departments array, and then modify the key-value pair of $employee, including changing the value of the "department" key to "Finance Department" , and assign the values ​​of the "leader" and "location" keys to the values ​​of the original department.

  1. Summary

By using the foreach statement, we can easily traverse multi-dimensional arrays and modify them. It should be noted that references & must be used when modifying subarrays.

The above is the detailed content of How to change the value of a 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