array_splice
array_splice
(PHP 4, PHP 5, PHP 7)
array_splice — Remove a portion of the array and replace it with something else
Description
array array_splice ( array &$input , int $offset [, int $length = count($input) [, mixed $replacement = array() ]] )
Removes the elements designated by offset
and length
from the input
array, and replaces them with the elements of the replacement
array, if supplied.
Note that numeric keys in input
are not preserved.
Note: If
replacement
is not an array, it will be typecast to one (i.e.(array) $replacement
). This may result in unexpected behavior when using an object orNULL
replacement
.
Parameters
-
input
-
The input array.
-
offset
-
If
offset
is positive then the start of removed portion is at that offset from the beginning of theinput
array. Ifoffset
is negative then it starts that far from the end of theinput
array. -
length
-
If
length
is omitted, removes everything fromoffset
to the end of the array. Iflength
is specified and is positive, then that many elements will be removed. Iflength
is specified and is negative then the end of the removed portion will be that many elements from the end of the array. Iflength
is specified and is zero, no elements will be removed. Tip: to remove everything fromoffset
to the end of the array whenreplacement
is also specified, use count($input) forlength
. -
replacement
-
If
replacement
array is specified, then the removed elements are replaced with elements from this array.If
offset
andlength
are such that nothing is removed, then the elements from thereplacement
array are inserted in the place specified by theoffset
. Note that keys in replacement array are not preserved.If
replacement
is just one element it is not necessary to put array() around it, unless the element is an array itself, an object orNULL
.
Return Values
Returns an array consisting of the extracted elements.
Examples
Example #1 array_splice() examples
<?php $input = array("red", "green", "blue", "yellow"); array_splice($input, 2); // $input is now array("red", "green") $input = array("red", "green", "blue", "yellow"); array_splice($input, 1, -1); // $input is now array("red", "yellow") $input = array("red", "green", "blue", "yellow"); array_splice($input, 1, count($input), "orange"); // $input is now array("red", "orange") $input = array("red", "green", "blue", "yellow"); array_splice($input, -1, 1, array("black", "maroon")); // $input is now array("red", "green", // "blue", "black", "maroon") $input = array("red", "green", "blue", "yellow"); array_splice($input, 3, 0, "purple"); // $input is now array("red", "green", // "blue", "purple", "yellow"); ?>
Example #2 array_splice() examples
The following statements change the values of $input the same way:
<?php array_push($input, $x, $y); array_splice($input, count($input), 0, array($x, $y)); array_pop($input); array_splice($input, -1); array_shift($input); array_splice($input, 0, 1); array_unshift($input, $x, $y); array_splice($input, 0, 0, array($x, $y)); $input[$x] = $y; // for arrays where key equals offset array_splice($input, $x, 1, $y); ?>
See Also
- array_slice() - Extract a slice of the array
- unset() - Unset a given variable
- array_merge() - Merge one or more arrays
© 1997–2017 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://secure.php.net/manual/en/function.array-splice.php