In PHP, you can use the array_splice() function to replace the value in the array. This function can remove the selected element from the array and replace it with a new element; the syntax format is "array_splice(array, The position to start deletion, the number of deleted elements, and the replacement value)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use array_splice () function to replace the value value in the array.
Example: Replace value value in array
<?php $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("a"=>"purple","b"=>"orange"); array_splice($a1,0,2,$a2); print_r($a1); ?>
Output:
Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )
Explanation:
array_splice() function is used to delete part of the elements of the array; you can delete them directly or replace them with other values.
If the function does not remove any elements (length=0), the replacement array will be inserted from the position of the start parameter
array_splice() The syntax is as follows:
array_splice(array,start,length,replacement)
Parameter description :
array represents an array.
start indicates the position (subscript) where deletion starts:
If start is a positive number, delete from front to back.
If start is a negative number, start from the position -start from the end of arr and delete from back to front. For example -2 means start from the second to last element of the array.
length is an optional parameter, indicating the number of elements to delete:
If length is a positive number, then It means deleting length elements;
If length is a negative number, then all elements starting from start and counting down to length from the end of the array will be deleted;
If omitted, all elements from start to the end of the array will be deleted.
#replacement is an optional parameter indicating the value to be replaced. If replacement has multiple values, it needs to be set to an array. If there is only one value, it does not need to be set to an array.
If the combination of start and length results in no element being deleted, then the value contained in replacement will be inserted into the position specified by start.
Note that using replacement to replace array elements will not retain the original key names.
Return value: Returns an array consisting of the deleted elements.
Example:
<?php $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 2); print_r($arr); //$arr 现在变成 array("red", "green") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, -1); print_r($arr); //$arr 现在变成 array("red", "yellow") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, count($arr), "orange"); print_r($arr); //$arr 现在变成 array("red", "orange") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, -1, 1, array("black", "maroon")); print_r($arr); //$input 现在变成 array("red", "green", "blue", "black", "maroon") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 3, 0, "purple"); print_r($arr); //$arr 现在变成 array("red", "green", "blue", "purple", "yellow"); ?>
Output:
Array ( [0] => red [1] => green ) Array ( [0] => red [1] => yellow ) Array ( [0] => red [1] => orange ) Array ( [0] => red [1] => green [2] => blue [3] => black [4] => maroon ) Array ( [0] => red [1] => green [2] => blue [3] => purple [4] => yellow )
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to replace value in php array. For more information, please follow other related articles on the PHP Chinese website!