This function removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied. It returns an array containing the extracted elements.
Note: The keys in the replaced array are not preserved.
Parameter | Description |
---|---|
array | Required. Specifies an array |
start | Required. Numeric value. Specifies where the function will start removing elements. 0 = the first element. If this value is set to a negative number, the function will start that far from the last element. -2 means start at the second last element of the array. |
length | Optional. Numeric value. Specifies how many elements will be removed, and also length of the returned array. If this value is set to a negative number, the function will stop that far from the last element. If this value is not set, the function will remove all elements, starting from the position set by the start-parameter. |
array | Optional. Specifies an array with the elements that will be inserted to the original array. If it’s only one element, it can be a string, and does not have to be an array. |
Syntax
array_splice(array,start,length,array)
Example
$array1=array("a"=>"Cooper","b"=>"Samuel","c"=>"Lachlan","d"=>"James");
$array2=array("a"=>"Prince","b"=>"Alex");
array_splice($array1,0,2,$array2);
print_r($array1);
Output
Array ( [0] => Prince [1] => Alex [c] => Lachlan [d] => James )