PHP Function Reference

PHP array_splice() Function



The PHP array_splice() function removes the elements specified by offset and length from the given array, and replaces them with the elements of the replacement array, if supplied.

Syntax

array_splice(array, offset, length, replacement)

Parameters

array Required. Specify the input array.
offset Required. Specify offset parameter which denotes the position in the array, not the key.
  • If it is non-negative, the removed portion will start at that offset in the array.
  • If it is negative, the removed portion will start that far from the end of the array.
length Optional. Specify how many elements will be removed, and also length of the returned array.
  • If length is a positive number, then the removed portion will have up to that many elements in it.
  • If the array is shorter than the length, then only the available array elements will be removed.
  • If length is a negative number then 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.
  • If it is omitted, then the removed portion will have everything from offset up until the end of the array.
replacement Optional. Specifies an array with the elements. If it has only one element, it can be a string, and does not have to be an array. If 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: Keys in the replacement array are not preserved.

Return Value

Returns an array consisting of the extracted elements.

Exceptions

NA.

Example:

The example below shows the usage of array_splice() function.

<?php
$Arr = array('red', 'green', 'blue', 'yellow');
//removed portion [2, end of array]
array_splice($Arr, 2);
print_r($Arr);

$Arr = array('red', 'green', 'blue', 'yellow');
//removed portion [1, end of array)
array_splice($Arr, 1, -1);
echo "\n";
print_r($Arr);

$Arr = array('red', 'green', 'blue', 'yellow');
//removed portion [1, end of array] 
//and insert replacement array
array_splice($Arr, 1, count($Arr), "orange");
echo "\n";
print_r($Arr);

$Arr = array('red', 'green', 'blue', 'yellow');
//removed portion [-1, -1] 
//and insert replacement array
array_splice($Arr, -1, 1, array("black", "maroon"));
echo "\n";
print_r($Arr);
?>

The output of the above code will be:

Array
(
    [0] => red
    [1] => green
)

Array
(
    [0] => red
    [1] => yellow
)

Array
(
    [0] => red
    [1] => orange
)

Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => black
    [4] => maroon
)

Example:

Consider the example below where an array with string keys is spliced.

<?php
$Arr = array('a'=>'red', 
             'b'=>'green', 
             'c'=>'blue', 
             'd'=>'yellow', 
             'e'=>'black', 
             'f'=>'white');

$replacement = array('a'=>'maroon', 
                     'b'=>'pink');

//removed portion [3, end of array]
//and insert replacement array, please
//note that keys in the replacement
//array are not preserved
echo "Spliced array:\n";
$removed = array_splice($Arr, 3, 3, $replacement);
print_r($Arr);
echo "\nRemoved portion:\n";
print_r($removed);
?>

The output of the above code will be:

Spliced array:
Array
(
    [a] => red
    [b] => green
    [c] => blue
    [0] => maroon
    [1] => pink
)

Removed portion:
Array
(
    [d] => yellow
    [e] => black
    [f] => white
)

Example: Equivalent statements to various array_splice() examples

The following statements are equivalent:

//append two elements to Arr
array_push($Arr, $x, $y);
array_splice($Arr, count($Arr), 0, array($x, $y));

//remove the last element of Arr
array_pop($Arr);
array_splice($Arr, -1);

//remove the first element of Arr
array_shift($Arr);
array_splice($Arr, 0, 1);

//insert an element at the start of Arr
array_unshift($Arr, $x, $y);
array_splice($Arr, 0, 0, array($x, $y));

//replace the value in Arr at index x
$Arr[$x] = $y; // for arrays where key equals offset
array_splice($Arr, $x, 1, $y);

❮ PHP Array Reference