Home > Article > Backend Development > What are the php array replacement functions?
php array replacement functions include: 1. array_replace() function, which uses the value of the following array to replace the value of the first array; 2. array_replace_recursive() function, which recursively uses the value of the following array to replace the first one. The value of the array; 3. array_splice() function.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
1.array_replace() function
array_replace() function replaces the value of the first array with the value of the subsequent array. The syntax is as follows:
array_replace(array1,array2,array3...)
If a key exists in the first array array1 and also exists in the second array array2, the value in the first array array1 will be replaced by the second array Replace the values in array2.
If a key only exists in the first array array1, it will remain unchanged.
If a key exists in the second array array2, but does not exist in the first array array1, the element will be created in the first array array1.
If multiple replacement arrays are passed, they will be processed in order, and the values of the subsequent arrays will overwrite the values of the previous arrays.
Example 1: If a key exists in array1 and also exists in array2, the value of the first array will be replaced by the value of the second array
<?php $a1=array("a"=>"red","b"=>"green"); $a2=array("a"=>"orange","burgundy"); print_r(array_replace($a1,$a2)); ?>
Output:
Array ( [a] => orange [b] => green [0] => burgundy )
Example 2: If a key only exists in the second array:
<?php $a1=array("a"=>"red","green"); $a2=array("a"=>"orange","b"=>"burgundy"); print_r(array_replace($a1,$a2)); ?>
Output:
Array ( [a] => orange [0] => green [b] => burgundy )
2, array_replace_recursive() function
array_replace_recursive() function recursively replaces the value of the first array with the value of the subsequent array. The syntax is as follows:
array_replace_recursive(array1,array2,array3...)
Example: Multiple arrays
<?php $a1=array("a"=>array("red"),"b"=>array("green","blue")); $a2=array("a"=>array("yellow"),"b"=>array("black")); $a3=array("a"=>array("orange"),"b"=>array("burgundy")); print_r(array_replace_recursive($a1,$a2,$a3)); ?>
Output:
Array ( [a] => Array ( [0] => orange ) [b] => Array ( [0] => burgundy [1] => blue ) )
Explanation: The difference between array_replace() and array_replace_recursive():
<?php $a1=array("a"=>array("red"),"b"=>array("green","blue"),); $a2=array("a"=>array("yellow"),"b"=>array("black")); $result=array_replace_recursive($a1,$a2); print_r($result); $result=array_replace($a1,$a2); print_r($result); ?>
Output:
Array ( [a] => Array ( [0] => yellow ) [b] => Array ( [0] => black [1] => blue ) ) Array ( [a] => Array ( [0] => yellow ) [b] => Array ( [0] => black ) )
3. array_splice() function
array_splice() function removes the selected element from the array and replaces it with the new element replace it. This function will also return the array containing the removed elements. If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter.
Syntax
array_splice(array,start,length,array)
Parameters | Description |
---|---|
array | Required. Specifies an array. |
start |
Required. numerical value. Specifies the starting position of deleted elements.
|
length |
Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array.
|
array |
Optional. Specifies the array with the elements to be inserted into the original array. If there is only one element, it can be set to a string and does not need to be set to an array. |
Example: Set the length parameter to 0:
<?php $a1=array("0"=>"red","1"=>"green"); $a2=array("0"=>"purple","1"=>"orange"); array_splice($a1,1,0,$a2); print_r($a1); ?>
Output:
Array ( [0] => red [1] => purple [2] => orange [3] => green )
Recommended learning: " PHP video tutorial》
The above is the detailed content of What are the php array replacement functions?. For more information, please follow other related articles on the PHP Chinese website!