How to replace two-dimensional array in php

WBOY
Release: 2023-05-19 20:33:37
Original
860 people have browsed it

In PHP, array is a very common and useful data structure. Two-dimensional arrays add one dimension to one-dimensional arrays, allowing for more flexible storage and manipulation of data. However, in actual applications, we may need to perform a replacement operation on a two-dimensional array, such as replacing a certain value in the array, and this replacement operation needs to be implemented in multiple sub-arrays at the same time. This article will introduce how to replace a two-dimensional array in PHP.

1. Replace a single value in a two-dimensional array

In PHP, we can use the array_replace() function to replace a single value in an array. This function accepts multiple arrays as parameters and returns a new array after replacement. For example, we have the following two-dimensional array:

$arr = array(
   array('name' => 'Tom', 'age' => 18),
   array('name' => 'Jack', 'age' => 20),
   array('name' => 'Mary', 'age' => 22)
);
Copy after login

If we need to change the information named "Tom" to "Peter", we can use the array_replace() function to achieve this:

$newArr = array_replace($arr[0], array('name' => 'Peter'));
$arr[0] = $newArr;
Copy after login

The above code , we first use the array_replace() function to replace the information named "Tom" with the information named "Peter", and assign the replaced new array to the $newArr variable. Then, we assign $newArr back to the original array again, realizing the operation of replacing an element in the two-dimensional array.

2. Replace multiple values ​​in the two-dimensional array

If we need to replace the same values ​​in multiple sub-arrays in the two-dimensional array, we can use the array_map() function to traverse array and replace it. An example is as follows:

$array = array(
 array('name' => 'Tom', 'age' => 18, 'sex' => 'M'),
 array('name' => 'Jack', 'age' => 20, 'sex' => 'M'),
 array('name' => 'Mary', 'age' => 22, 'sex' => 'F')
);

function replace_name($array) {
 $array['name'] = 'Peter';
 return $array;
}

$new_arr = array_map('replace_name', $array);
Copy after login

In the above code, we define a function named replace_name, which accepts an array as a parameter and changes the value corresponding to the "name" key in the array to "Peter" return. We then use the array_map() function to apply the function to each sub-array of the original array and assign the processed results to the $newArr variable. Finally, we can use the print_r() function to output the contents of the new array to the screen for viewing.

3. Use references to implement replacement

In PHP, you can use references (&) to change the value of the actual parameter in a function call. Therefore, we can also use references to replace data in a two-dimensional array. For example:

$array = array(
 array('name' => 'Tom', 'age' => 18, 'sex' => 'M'),
 array('name' => 'Jack', 'age' => 20, 'sex' => 'M'),
 array('name' => 'Mary', 'age' => 22, 'sex' => 'F')
);

function replace_name(&$array) {
 $array['name'] = 'Peter';
}

foreach($array as &$value) { replace_name($value); }
Copy after login

In the above code, we define a replace_name function that uses a reference to pass an array as a parameter and replaces the "name" key in the array with "Peter". We then iterate through the original array through a foreach loop and pass each element in turn to the replace_name function using a reference. Finally, the value corresponding to the "name" key in the original array was successfully replaced with "Peter".

In summary, to replace a two-dimensional array in PHP, you can use the array_replace() function, array_map() function, or pass parameters by reference and achieve it through a loop. Different methods are suitable for different scenarios, and you can choose flexibly according to the actual situation.

The above is the detailed content of How to replace two-dimensional 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