Home > Article > Backend Development > How to replace a certain element in an array in php
In PHP, an array is a commonly used data structure used to store multiple values. Sometimes we need to replace one of the values. This article will introduce how to implement this function in PHP.
There are many ways to replace a certain value in an array in PHP. Three of them are introduced below.
The subscript in the PHP array can be a number or a string, and we can directly access the value in the array through the subscript. Therefore, the easiest way is to use array subscripts to replace the corresponding values.
// 以数字下标为例 $array = [0,1,2]; $array[1] = "one"; print_r($array); // Array ( [0] => 0 [1] => one [2] => 2 ) // 以字符串下标为例 $array = ['name'=>'Amy', 'age'=>18]; $array['age'] = 20; print_r($array); // Array ( [name] => Amy [age] => 20 )
In the above code, we use the subscript of the array to modify the value in the array. This method is suitable for situations where the subscript corresponding to the value to be replaced is known.
If you don’t know the subscript of the value to be replaced, you can use the array_search() function to find the position of the value in the array. The array_search() function returns the subscript of the first occurrence of the element in the array, or false if it is not found.
$array = ['apple', 'banana', 'orange']; $key = array_search('banana', $array); // $key = 1 if($key !== false){ $array[$key] = 'pear'; } print_r($array); // Array ( [0] => apple [1] => pear [2] => orange )
In the above code, we first find the subscript corresponding to the element with the value 'banana' in the array, and then replace the value corresponding to the subscript with 'pear'.
It should be noted that if the value to be replaced appears multiple times in the array, the above method can only replace the first occurrence of the value. If you want to replace all occurrences of the value, you need to use a loop to traverse the array.
If you want to replace multiple values at the same time, we can use the array_replace() function, which replaces the original array with the value of each element in the new array. values with the same key. If the key does not exist, a new key-value pair is added to the array.
$array1 = ['fruit1'=>'apple', 'fruit2'=>'banana']; $array2 = ['fruit2'=>'orange', 'fruit3'=>'pear']; $result = array_replace($array1, $array2); print_r($result); // Array ( [fruit1] => apple [fruit2] => orange [fruit3] => pear )
In the above code, we use two arrays to store the key-value pairs to be replaced, and then use the array_replace() function to replace the key-value pairs in array 2 with the value of the same key in array 1. If the key-value pair with key 'fruit3' does not exist in the original array, the key-value pair is added to the array.
Summary
In PHP, replacing any element of an array is a common task. This article introduces three methods: replacing by key name, using array_search() function and using array_replace() function, which are suitable for different situations. In implementation, these methods can be flexibly used according to actual needs to improve the efficiency and accuracy of array operations.
The above is the detailed content of How to replace a certain element in an array in php. For more information, please follow other related articles on the PHP Chinese website!