In the PHP programming language, array is a very common and practical data type. In arrays, we can quickly access and modify array elements based on subscripts. But in actual programming, we often need to replace elements in an array. At this time, PHP's built-in array replacement function is very convenient and practical.
This article will introduce in detail the array replacement functions in PHP, including array_replace(), array_replace_recursive(), array_merge(), array_merge_recursive() and array_combine() and other functions.
1. array_replace()
array_replace() function is used to replace elements in one array with elements in another array. If there are duplicate keys, the subsequent values will be overwritten. previous value. The syntax of this function is as follows:
array array_replace ( array $array1 , array $array2 [, array $... ] )
Among them, $array1 is the original array, $array2 is the array to be replaced, and $... indicates that multiple arrays can be passed in. This function will return a replaced array.
The following is a sample code:
<?php $array1 = array("a" => "apple", "b" => "banana", "c" => "cherry"); $array2 = array("b" => "blueberry", "c" => "coconut"); $result = array_replace($array1, $array2); print_r($result); ?>
The output of this code is as follows:
Array ( [a] => apple [b] => blueberry [c] => coconut )
As you can see, the elements in array $array2 have replaced array $array1 corresponding elements.
2. array_replace_recursive()
The usage of array_replace_recursive() function is basically the same as array_replace(), but it will recursively replace the elements in the array. The syntax of this function is as follows:
array array_replace_recursive ( array $array1 , array $array2 [, array $... ] )
Unlike array_replace(), the replacement operation of this function is performed recursively. For example, if there are two arrays:
$array1 = array("a" => array("b" => "banana", "c" => "cherry")); $array2 = array("a" => array("b" => "blueberry", "d" => "date"));
If we use the array_replace() function, the result is like this:
$result = array_replace($array1, $array2); print_r($result);
Output result:
Array ( [a] => Array ( [b] => blueberry [c] => cherry [d] => date ) )
As you can see, $ The elements in array2 successfully replaced the elements in $array1, but the element "c" that originally belonged to $array1 was deleted. This is because the array_replace() function simply replaces the elements in $array2 with the elements in $array1, and does not take the subarray into account.
If we use the array_replace_recursive() function to replace these two arrays, the result is like this:
$result = array_replace_recursive($array1, $array2); print_r($result);
Output result:
Array ( [a] => Array ( [b] => blueberry [c] => cherry [d] => date ) )
You can see that the elements in $array1 "c" is successfully retained, and the element "d" in $array2 is also successfully added to the result array, which means that the array_replace_recursive() function recursively retains all elements in both arrays.
3. array_merge()
The array_merge() function is used to merge multiple arrays into a new array and remove duplicate elements. The syntax of this function is as follows:
array array_merge ( array $array1 [, array $... ] )
Among them, $array1 is the first array, and multiple arrays can be passed in. This function returns the new merged array.
The following is a sample code:
<?php $array1 = array("a" => "apple", "b" => "banana"); $array2 = array("b" => "blueberry", "c" => "cherry"); $result = array_merge($array1, $array2); print_r($result); ?>
The output of this code is as follows:
Array ( [a] => apple [b] => blueberry [c] => cherry )
As you can see, this function merges the elements in the two arrays into A new array with duplicate elements removed.
4. array_merge_recursive()
The usage of array_merge_recursive() function is similar to array_merge(), except that it will recursively merge the elements in the array. The syntax of this function is as follows:
array array_merge_recursive ( array $array1 [, array $... ] )
Unlike array_merge(), the merge operation of this function is performed recursively. For example, if there are two arrays:
$array1 = array("a" => array("b" => "banana")); $array2 = array("a" => array("c" => "cherry"));
If we use the array_merge() function to merge the two arrays, the result is like this:
$result = array_merge($array1, $array2); print_r($result);
Output result:
Array ( [a] => Array ( [c] => cherry ) )
As you can see, the elements in $array2 were successfully merged into the result array, but the element "b" that originally belonged to $array1 was deleted. This is because the array_merge() function simply merges the elements in the two arrays without taking into account the subarrays.
If we use the array_merge_recursive() function to merge these two arrays, the result is like this:
$result = array_merge_recursive($array1, $array2); print_r($result);
Output result:
Array ( [a] => Array ( [b] => banana [c] => cherry ) )
As you can see, $array1 and $array2 The elements in are successfully merged into the result array, which means that the array_merge_recursive() function recursively merges all the elements in the two arrays.
5. array_combine()
The array_combine() function is used to use the key names in one array as the values in another array to generate a new array. The syntax of this function is as follows:
array array_combine ( array $keys , array $values )
Among them, $keys is an array containing key names, and $values is an array containing key values. This function will return a new array with the keys from the $keys array and the key values from the $values array.
The following is a sample code:
<?php $keys = array("a", "b", "c"); $values = array("apple", "banana", "cherry"); $result = array_combine($keys, $values); print_r($result); ?>
The output of this code is as follows:
Array ( [a] => apple [b] => banana [c] => cherry )
As you can see, this function converts the elements in the $keys and $values arrays Paired together, a new array is generated.
Summary
This article introduces the array replacement functions in PHP, including array_replace(), array_replace_recursive(), array_merge(), array_merge_recursive() and array_combine() and other functions. These functions are very useful in actual programming and can help us quickly operate and process arrays and improve the quality and efficiency of the code.
The above is the detailed content of php array replacement function. For more information, please follow other related articles on the PHP Chinese website!