Home  >  Article  >  Backend Development  >  How to change the value in a two-dimensional array in php

How to change the value in a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-17 15:25:57993browse

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!

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