array_splice
(PHP 4, PHP 5, PHP 7)
array_splice — Remove a portion of the array and replace it with something else
Description
&$input
, int $offset
[, int $length
= count($input)
[, mixed $replacement
= array()
]] ) : 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:
Numerical 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 the removed portion is at that offset from the beginning of theinput
array.If
offset
is negative then the start of the removed portion is at that offset from the end of theinput
array. -
length
-
If
length
is omitted, removes everything fromoffset
to the end of the array.If
length
is specified and is positive, then that many elements will be removed.If
length
is specified and is negative, then the end of the removed portion will be that many elements from the end of the array.If
length
is specified and is zero, no elements will be removed.TipTo remove everything from
offset
to the end of the array whenreplacement
is also specified, usecount($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:
Keys in the
replacement
array are not preserved.If
replacement
is just one element it is not necessary to put array() or square brackets around it, unless the element is an array itself, an object orNULL
.
Examples
Example #1 array_splice() examples
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
var_dump($input);
?>
The above example will output:
array(2) { [0]=> string(3) "red" [1]=> string(5) "green" } array(2) { [0]=> string(3) "red" [1]=> string(6) "yellow" } array(2) { [0]=> string(3) "red" [1]=> string(6) "orange" } array(5) { [0]=> string(3) "red" [1]=> string(5) "green" [2]=> string(4) "blue" [3]=> string(5) "black" [4]=> string(6) "maroon" }
Example #2 Equivalent statements to various array_splice() examples
The following statements are equivalent:
<?php
// append two elements to $input
array_push($input, $x, $y);
array_splice($input, count($input), 0, array($x, $y));
// remove the last element of $input
array_pop($input);
array_splice($input, -1);
// remove the first element of $input
array_shift($input);
array_splice($input, 0, 1);
// insert an element at the start of $input
array_unshift($input, $x, $y);
array_splice($input, 0, 0, array($x, $y));
// replace the value in $input at index $x
$input[$x] = $y; // for arrays where key equals offset
array_splice($input, $x, 1, $y);
?>
See Also
- array_merge() - Merge one or more arrays
- array_slice() - Extract a slice of the array
- unset() - Unset a given variable
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-array-splice.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.