How to use the PHP array_splice function?

PHP array_splice() function

The PHP array_splice() function is used to remove a portion of an array and replace that portion with something else.

array_splice(array, start, length, replacement)
  • array (Required) – The array whose fraction of elements you want to remove.
  • start (Required) – This parameter takes the position number of the element from where you want to start removing.
  • length (Optional) – The length defines how many elements you want to remove from the starting number. If length is omitted, removes everything from starting position to the end of the array. But, if you don’t want to remove any element, then pass 0.
  • replacement (Optional) – If you specify an array into this parameter, then the removed elements will be replaced with elements from this array. But it is not mandatory that you have to pass only array type of data, you could pass a string, int, etc.

This array_splice() function returns the removed elements, and it performs the changes in the original array.

Example of the array_splice() function

In the following example, you can see that the green and blue colors are removed from the array because we have specified to remove 2 elements from the position 1.

<?php
//                0       1        2       3         4
$colors = array("red", "green", "blue", "yellow", "orange");

$removedEl = array_splice($colors, 1, 2);

print_r($colors);
echo "\n# - Removed Element\n";
print_r($removedEl);
Array
(
    [0] => red     
    [1] => yellow  
    [2] => orange  
)

# - Removed Element
Array
(
    [0] => green   
    [1] => blue    
)

Adding elements of a new array in place of removed elements

<?php
//                0       1        2       3         4
$colors = array("red", "green", "blue", "yellow", "orange");
$newColors = array("purplle", "pink", "white");

$removedEl = array_splice($colors, 1, 2, $newColors);

print_r($colors);
echo "\n# - Removed Element\n";
print_r($removedEl);
Array
(
    [0] => red     
    [1] => purplle 
    [2] => pink    
    [3] => white   
    [4] => yellow  
    [5] => orange  
)

# - Removed Element
Array
(
    [0] => green   
    [1] => blue    
)

It is not mandatory that the replacement value must be an array.

<?php
//                0       1        2       3         4
$colors = array("red", "green", "blue", "yellow", "orange");

$removedEl = array_splice($colors, 1, 2, 'Black');

print_r($colors);
echo "\n# - Removed Element\n";
print_r($removedEl);
Array
(
    [0] => red     
    [1] => Black   
    [2] => yellow  
    [3] => orange  
)

# - Removed Element
Array
(
    [0] => green   
    [1] => blue    
)

Just insert new elements

If you want to insert only new elements at a specific position and don’t want to remove any elements, you have to pass 0 in the length parameter.

<?php
//                0       1        2       3         4
$colors = array("red", "green", "blue", "yellow", "orange");

$removedEl = array_splice($colors, 2, 0, 'Black');

print_r($colors);
echo "\n# - Removed Element\n";
print_r($removedEl);
Array
(
    [0] => red     
    [1] => green   
    [2] => Black   
    [3] => blue    
    [4] => yellow  
    [5] => orange  
)

# - Removed Element
Array
(
)

One thing you have to note is that the numeric keys in the array will not be preserved.

Leave a Reply