IMPRESS dotted_line CONTACT dotted_line search dotted_line Kontakt dotted_line sitemap dotted_line print
119 PHP-Resource users online

Switch to another languags Deutsch aktuelle Sprache Englisch

PHP Manual


(PHP 4, PHP 5)

array_spliceRemove a portion of the array and replace it with something else


array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement ]] )

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) $parameter). This may result in unexpected behavior when using an object or NULL replacement.



The input array.


If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far from the end of the input array.


If length is omitted, removes everything from offset 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. Tip: to remove everything from offset to the end of the array when replacement is also specified, use count($input) for length.


If replacement array is specified, then the removed elements are replaced with elements from this array.

If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset. 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 or NULL.

Return Values

Returns the array consisting of the extracted elements.


Example #1 array_splice() examples

= array("red""green""blue""yellow");
// $input is now array("red", "green")

$input = array("red""green""blue""yellow");
array_splice($input1, -1);
// $input is now array("red", "yellow")

$input = array("red""green""blue""yellow");
array_splice($input1count($input), "orange");
// $input is now array("red", "orange")

$input = array("red""green""blue""yellow");
array_splice($input, -11, array("black""maroon"));
// $input is now array("red", "green",
//          "blue", "black", "maroon")

$input = array("red""green""blue""yellow");
// $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:

array_splice($inputcount($input), 0, array($x$y));
array_splice($input, -1);
array_splice($input00, array($x$y));
$input[$x] = $y// for arrays where key equals offset

See Also

Comments to the PHP manual
Write new comment


New Tutorial entries

Migration einer PHP 5 App auf PHP 7

Dieses PHP 7 Tutorial zeigt dir, wie du dein PHP5 Script auf PHP7 umstellst.

Berni | Category: PHP
PHP 7 Virtual Machine

Dieser Artikel zielt darauf ab, einen Überblick über die Zend Virtual Machine, wie es in PHP 7 gefunden wird.

Berni | Category: PHP
plotting masters - a professional guide - Teil II

Grafische Interpolation und Bestapproximation von numerischen Wertepaaren: Wir wollen Punkte auf einer Zeichenebene über verschiedene Verfahren miteinander verbinden.

EVAMasters | Category: PHP