The php array_splice() function gives us the ability to remove elements from an array by their position, insert new elements at a specific position, and replace elements.

To remove an element from an array, pass an array and the position of the element you want to remove to array_splice()

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Remove element at position 1
array_splice($array, 1);
print_r($array);

// Output: 
Array
(
    [0] => elephant
    [1] => rabbit
    [2] => salmon
    [3] => alligator
    [4] => whale
)

To replace elements in an array with array_splice(), pass an array, the starting position of where you will replace, the number of elements you want to remove, and then the elements you want to insert.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Replace element at position 2
array_splice($array, 2, 1, "monkey");
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => monkey
    [3] => salmon
    [4] => alligator
    [5] => whale
)

To insert elements in an array with array_splice(), pass an array, the starting position of where you will insert, 0 to make 0 deletions, and then the elements you want to insert.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Insert element at position 3
array_splice($array, 3, 0, "monkey");
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => rabbit
    [3] => monkey
    [4] => salmon
    [5] => alligator
    [6] => whale
)

When working with collections of data in php, the ability to add to, remove from, and manipulate those collections is valuable.

The php array_splice() function allows us to do all of this. With array_splice(), you can remove elements based on their position, insert new elements at a specific position, and also make replacements.

array_splice() takes two required parameter and two optional parameters. The required parameters are the array you want to operate on and the starting position of the portion you want to remove.

The optional parameters are the length of the portion you want to remove and the elements you want to insert.

So, if you want to remove an element from an array, pass an array and the position of the element you want to remove to array_splice().

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Remove element at position 1
array_splice($array, 1);
print_r($array);

// Output: 
Array
(
    [0] => elephant
    [1] => rabbit
    [2] => salmon
    [3] => alligator
    [4] => whale
)

If you want to remove multiple elements, also pass the number of elements you want to remove.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Remove element at position 1
array_splice($array, 1, 2);
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => salmon
    [2] => alligator
    [3] => whale
)

Using array_splice() to Replace Elements in Array in php

You can make replacements in arrays with array_splice().

To replace elements in an array with array_splice(), pass an array, the starting position of where you will replace, the number of elements you want to remove, and then the elements you want to insert.

Below is an example of how to replace an element in an array with array_splice() in php.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Replace element at position 2
array_splice($array, 2, 1, "monkey");
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => monkey
    [3] => salmon
    [4] => alligator
    [5] => whale
)

If you want to replace more than one element, pass an array to array_splice().

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Replace two elements at position 2
array_splice($array, 2, 2, array("monkey","giraffe"));
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => monkey
    [3] => giraffe
    [4] => alligator
    [5] => whale
)

Using array_splice() to Insert Elements in Array in php

You can make insertions in an array with array_splice().

To insert elements in an array with array_splice(), pass an array, the starting position of where you will insert, 0 to make 0 deletions, and then the elements you want to insert.

Below is an example of how to insert elements into an array using array_splice() in php.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Insert element at position 3
array_splice($array, 3, 0, "monkey");
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => rabbit
    [3] => monkey
    [4] => salmon
    [5] => alligator
    [6] => whale
)

Hopefully this article has been useful for you to learn how to use array_splice() to remove, insert and replace elements in arrays in your php programs.

Categorized in:

PHP,

Last Update: February 26, 2024