PHP's array_splice Function

The PHP array_splice function can be used to add elements, remove elements, and/or replace elements in an array. An offset argument allows you to specify where in the array to make changes. The array_splice function modifies the array passed as its first argument, and it returns an array of removed elements. The modified array is re-indexed starting from 0.

array_splice Arguments

The first argument to array_splice is the array you wish to modify. The second argument is the offset, i.e., the location in the array where elements are to be added, removed, or replaced. The (optional) third argument is the number of elements to remove from the array. The fourth argument is an array of elements you wish to add.

How to Add Elements to an Array with array_splice

The example below demonstrates how to use the array_splice function to insert new elements anywhere you like in an existing array:

$ar = array('Morie', 'Miki', 'Coco', 'Halo');
// array_splice arguments: array to modify, offset (where to insert), 
// number of elements to remove, array of elements to add
array_splice($ar, 2, 0, ['Mittens', 'Pumpkin'] );
print_r($ar);
/* print_r output (as seen in page source view):
Array
(
    [0] => Morie
    [1] => Miki
    [2] => Mittens
    [3] => Pumpkin
    [4] => Coco
    [5] => Halo
) */

When you want to use array_splice to add elements to an array, but not to remove or replace, pass 0 as the third argument.

The array_splice function supports a negative offset which allows you to specify a location starting from the end of the array. The following demonstrates how to insert two elements one element from the end of the array:

$ar = array('Morie', 'Miki', 'Coco', 'Halo');
// array_splice with negative offset
array_splice($ar, -1, 0, ['Rudi', 'Muffin']);
print_r($ar);
/* Array
(
    [0] => Morie
    [1] => Miki
    [2] => Coco
    [3] => Rudi
    [4] => Muffin
    [5] => Halo
) */

How to Remove Elements from an Array with array_splice

The array_splice function can be used to remove elements anywhere in an array. The example below demonstrates removing two elements from an array, starting from an offset of 3 (i.e., the 4th element):

$ar = array('Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin', 'Coco');
$r = array_splice($ar, 3, 2);
// view array returned by array_splice (array of removed elements)
print_r($r);
/* Array
(
    [0] => Miki
    [1] => Mittens
)
*/

// view modified $ar
print_r($ar);
/* Array
(
    [0] => Rudi
    [1] => Morie
    [2] => Halo
    [3] => Pumpkin
    [4] => Coco
)
*/

If you leave out the third argument, the elements from the offset to the end of the array will be removed:

$ar = array('Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin', 'Coco');
// remove from offset 4 to end of $ar
$r = array_splice($ar, 4);
// view array returned by array_splice
print_r($r);
/* Array
(
    [0] => Mittens
    [1] => Pumpkin
    [2] => Coco
) */

// view modified $ar
print_r($ar);
/* Array
(
    [0] => Rudi
    [1] => Morie
    [2] => Halo
    [3] => Miki
) */

The following example demonstrates using a negative offset to remove the last three elements from an array:

$ar = array('Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin', 'Coco');
// array_splice with negative offset
$r = array_splice($ar, -3, 3);

// display array returned by array_splice
print_r($r);
/* Array
(
    [0] => Mittens
    [1] => Pumpkin
    [2] => Coco
) */

// view modified $ar
print_r($ar);
/* Array
(
    [0] => Rudi
    [1] => Morie
    [2] => Halo
    [3] => Miki
) */

How to Replace Elements in an Array with array_splice

Finally, we demonstrate using array_splice to replace elements in an array. We remove two elements and replace with one:

$ar = array('Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin', 'Coco');

// offset 3, remove 2, replace with single element
$r = array_splice($ar, 3, 2, ['Muffin'] );

// display array returned by array_splice
print_r($r);
/* Array
(
    [0] => Miki
    [1] => Mittens
)
*/

// view modified $ar
print_r($ar);
/* Array
(
    [0] => Rudi
    [1] => Morie
    [2] => Halo
    [3] => Muffin
    [4] => Pumpkin
    [5] => Coco
)
*/

Back to top