In PHP, two-dimensional array is a common data type. It consists of multiple arrays, each of which can contain multiple keys and values. When we need to change the value of an element in a two-dimensional array, we can use some functions and syntax provided by PHP to achieve this. In this article, we will discuss how to change the values in a two-dimensional array.
1. Use foreach loop
In PHP, it is very common to use foreach loop to traverse a two-dimensional array. If we need to change the value of an element, we can use pass by reference in the foreach loop. Passing by reference means passing the address of the variable, not the value of the variable. This way, after modifying the value of the variable in the loop, the original array will also be modified.
For example, we have the following two-dimensional array:
$students = array( array('name' => 'Tom', 'age' => 18), array('name' => 'Jerry', 'age' => 20), array('name' => 'Lucy', 'age' => 22) );
We need to change Tom’s age from 18 to 19, you can use the following code:
foreach ($students as &$student) { if ($student['name'] == 'Tom') { $student['age'] = 19; } }
In the above In the code, we use reference transfer &
, and modify it in the loop by judging the value of the name
key.
2. Use the array_map function
In addition to using the foreach loop, we can also use the array_map function provided by PHP to change the values in the two-dimensional array. The array_map function can accept a function as an argument and apply the function to each element of the array. This function can return a modified element, thereby changing the value in the original array.
For example, we have the following two-dimensional array:
$prices = array( array('product' => 'pen', 'price' => 1.5), array('product' => 'pencil', 'price' => 0.5), array('product' => 'book', 'price' => 8) );
We need to multiply the prices by 2, you can use the following code:
function double_price($item) { $item['price'] *= 2; return $item; } $prices = array_map("double_price", $prices);
In the above code, We define a function double_price
that multiplies the price of the input element by 2 and returns the modified element. We then call this function using the array_map function and store the result back into the original array.
3. Use the array_walk_recursive function
For multi-dimensional arrays or two-dimensional arrays containing sub-arrays, we can use the array_walk_recursive function provided by PHP to change the value. Similar to array_map, array_walk_recursive also accepts a function as an argument and applies the function to each element of the array. The difference is that this function is applied recursively to each element of the array, including elements in subarrays.
For example, we have the following multidimensional array:
$marks = array( 'Tom' => array('Chinese' => 80, 'Math' => 90), 'Jerry' => array('Chinese' => 85, 'Math' => 95) );
We need to change Jerry’s math score from 95 to 100, you can use the following code:
function update_jerry(&$value, $key) { if ($key == 'Jerry' && is_array($value) && isset($value['Math'])) { $value['Math'] = 100; } } array_walk_recursive($marks, 'update_jerry');
In the above In the code, we define a function update_jerry
, which modifies the qualified value in the input element to 100. We then call this function using the array_walk_recursive function to store the result back into the original array.
Conclusion
In PHP, there are many ways to change the values in a two-dimensional array. We can use foreach loop, array_map function or array_walk_recursive function to achieve this. Different methods are suitable for different situations, and we can choose the most appropriate method according to specific needs. No matter which method we use, we should pay attention to following best practices to ensure the readability and maintainability of the code.
The above is the detailed content of How to change the value in a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
