Skip to content
Home » Functions » array_splice: How to use with examples

array_splice: How to use with examples

What is array_splice?

Description

PHP array_splice removes a portion of an array and replaces it with something else.

Function Signature

array_splice( array &$array, int $offset, ?int $length = null, mixed $replacement = [] ) : array

Arguments

  • $array – Input array
  • $offset 
    • Positive offset: The replacement will start from the start of the array.
    • Negative offset: The replacement will start from the end of the array.
  • $length
    • Omitted length: It removes everything from offset to the end of the array.
    • Positive length: It removes that many elements will be removed.
    • Negative length: It removes that many elements from the end of the array.
    • Zero length: No elements will be removed.
  • $replacement
    • If specified, it replaces the removed elements with the replacement array.
    • If nothing is removed, the replacement elements are inserted at the offset.

Return Type

Returns the removed/replaced array.

Notes

  • Keys in the array and replacement are not preserved.
  • If the replacement is not an array, it will be typecasted to an array, leading to unexpected behaviour with objects or nulls.
  • If replacement is just one element, it doesn’t require an array. The function typecasts it to an array.
  • The array_splice makes in place modifications to the input array.

Introduction – PHP array_splice

PHP array_splice is a versatile function for manipulating array elements. It has offset and length arguments for specifying a portion for removal. It also takes a replacement array as an argument that could be inserted at the offset or the removed portion. So, it does removal and replacement at the one call.

PHP array_splice

The array_splice in PHP can replicate functions like array_push, array_pop, array_shift and array_unshift, and the article includes a related example. The article also includes examples of how to use array_splice in PHP. By the end of this article, you’ll be well familiar with the array_splice functions. So, let’s start without any further ado.

Usage Examples – PHP array_splice

Here are some examples of the array_splice function with varying offsets and lengths. 

Example#1

<?php
$arr = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
 
//The array splice modified the input array.
array_splice($arr, 1, 3);
print_r($arr);
 
/*
Array
(
    [0] => 0
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 7
    [5] => 8
    [6] => 9
)
*/
?>

Example #1 is straightforward. There’s no replacement array but an offset of 1 and a length of 3. It starts from index 1, up to index 3, including three elements as specified by the length argument. So, the function removes three elements [1,2,3] from the input array.

Example#2

<?php
$arr = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
 
//The array splice modified the input array.
array_splice($arr, 1, 3, ["One", "Two", "Three"]);
print_r($arr);
 
/*
Array
(
    [0] => 0
    [1] => One
    [2] => Two
    [3] => Three
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
)
*/
?>

This example is almost similar to the first example except that there is a replacement array now. So, the array_splice removes the elements from the input array and replaces it with the replacement array.

Example#3

Sometimes the function is required to add elements to the array only. To do that, the length argument should be zero. Here’s how.

<?php
$arr = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
 
//The array splice modified the input array.
array_splice($arr, 1, 0, ["One", "Two", "Three"]);
print_r($arr);
 
/*
Array
(
    [0] => 0
    [1] => One
    [2] => Two
    [3] => Three
    [4] => 1
    [5] => 2
    [6] => 3
    [7] => 4
    [8] => 5
    [9] => 6
    [10] => 7
    [11] => 8
    [12] => 9
)
*/
?>

Because the zero length indicates no removal that’s why the function just adds the replacement array at offset 1.

So, these are some examples of the PHP array_splice. Next, let’s check out how it can be equivalent to some popular array functions like push, pop, shift, and unshift.

PHP array_splice rules them all…

The array_splice can do array push, pop, shift, and unshift. Here’s how.

PHP array_splice vs array_push

The array_push in PHP appends an element to an array. The array_splice can do this if the offset is set to the count of an array. The offset exceeds the last index and the function thus removes nothing but appends instead.

<?php
$arr = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
$arr2 = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
 
array_push($arr, 10);
array_splice($arr2,count($arr),0,10);
print_r($arr);
print_r($arr2);
 
/*
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
)
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
)
 
*/
?>

PHP array_splice vs array_pop

The array pop returns the last element of an array. The array_splice removes and returns the last element if the offset is -1.

<?php
$arr = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
$arr2 = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
 
$popped_elem = array_pop($arr);
$popped_elem2 = array_splice($arr2,-1);
 
print_r($popped_elem); //OUTPUT 9
print_r($popped_elem2[0]); //OUTPUT 9
 
?>

PHP array_splice vs array_unshift

The array_unshift prepends a value to an array. The array_splice can do so if the offset and length are zero, specifying addition at index 0.

<?php
$arr = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
$arr2 = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
 
array_unshift($arr,-1);
array_splice($arr2,0,0,-1);
 
print_r($arr);
print_r($arr2);
 
/*
OUTPUT
Array
(
    [0] => -1
    [1] => 0
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 4
    [6] => 5
    [7] => 6
    [8] => 7
    [9] => 8
    [10] => 9
)
Array
(
    [0] => -1
    [1] => 0
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 4
    [6] => 5
    [7] => 6
    [8] => 7
    [9] => 8
    [10] => 9
)
*/
?>

PHP array_splice vs array_shift

The array_shift returns the first element of an array. Specifying offset zero and length one removes and returns the first element in array_splice.

<?php
$arr = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
$arr2 = [0, 1, 2, 3, 4 ,5 ,6 ,7, 8, 9];
 
$elem = array_shift($arr);
$elem2 = array_splice($arr2,0,1);
 
print_r($elem); //OUTPUT 0
print_r($elem2[0]); //OUTPUT 0
 
?>

So, the array_splice PHP is a versatile function, being able to do multiple functions. 

Conclusion

This article explains the array_splice in PHP with examples. The article also includes examples to show array_splice examples equivalent to array push, pop, shift and unshift. Hopefully, you are now familiar with the function. If you have liked this article, you may like other similar PHP articles and tutorials at FuelingPHP. Do check them out!!

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.