PHP array_splice() Function

Usage — The PHP array_splice() function is used to remove a portion of the array and replace it with something else.

It has the following syntax:

PHP

array array_splice ( array &$input , int $offset [, int $length = count($input) [, mixed $replacement = array() ]] )

Here is an example of using array_splice():

PHP

$theme_a = array("a"=>"violet","b"=>"indigo","c"=>"blue","d"=>"green");
$theme_b = array("a"=>"yellow","b"=>"orange");
array_splice($theme_a, 1, 2, $theme_b);

print_r($theme_a);
/* Output —
Array
(
    [a] => violet
    [0] => yellow
    [1] => orange
    [d] => green
) */

Return Value — This function returns an array consisting of the extracted elements.

Additional Information — You should remember that if the length parameter in array_splice() is set to zero, no elements will be removed. In such cases, the replacement array will be inserted from the position of the offset parameter. Also keep in mind that the keys in the replacement array are not preserved.

Keep in mind that numeric keys in input are not preserved.

It should be noted that if replacement is not an array, it will be typecast to one (i.e. (array) $replacement). This may result in unexpected behavior when using an object or NULL replacement.

PHP Version and Changelog — The array_splice() function is available in PHP 4, PHP 5, PHP 7.

Relevant Functions — Other related PHP functions that you should know about are: array_slice() which extracts a slice of the array, unset which unsets a given variable and array_merge() which merges one or more arrays.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

input

The input parameter is used to specify the input array. This is a required parameter.

offset

The offset parameter is used to specify the point from which the function should start removing elements. If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far from the end of the input array. This is a required parameter.

length

The length parameter is used to specify the number of elements that should be removed. This is an optional parameter.

If you omit the length, the function removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array. In other words, a negative length means that the removal will stop at length distance from the last element. If length is specified and is zero, no elements will be removed.

replacement

The replacement parameter is used to specify a replacement array that takes place of the removed elements in the original array. This is an optional parameter.

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. Please note that keys in replacement array are not preserved. If it’s only one element, it can be a string, and does not have to be an array.

Working Examples

Here are some examples of using the array_splice() function:

PHP

$theme_a = array("violet", "indigo", "blue", "green");
$theme_b = array("yellow", "orange");
array_splice($theme_a, 1, 1, $theme_b);

print_r($theme_a);
/* Output —
Array
(
    [0] => violet
    [1] => yellow
    [2] => orange
    [3] => blue
    [4] => green
) */

The following example shows you how the function behaves with different values of length and offset. You should try to figure out the output in each case yourself to see if you understand the function correctly.

PHP

$colors = array("red", "green", "blue", "yellow");
print_r(array_splice($colors, 2));
/* Output —
Array
(
    [0] => blue
    [1] => yellow
) */

$colors = array("red", "green", "blue", "yellow");
print(array_splice($colors, 1, -1));
/* Output —
Array
(
    [0] => green
    [1] => blue
) */

$colors = array("red", "green", "blue", "yellow");
print_r(array_splice($colors, 1, count($colors), "orange"));
/* Output —
Array
(
    [0] => green
    [1] => blue
    [2] => yellow
) */

$colors = array("red", "green", "blue", "yellow");
print_r(array_splice($colors, -1, 1, array("black", "maroon")));
/* Output —
Array
(
    [0] => yellow
) */

$colors = array("red", "green", "blue", "yellow");
print_r(array_splice($colors, 3, 0, "purple"));
/* Output —
Array
(
) */

The following example shows how you can use array_splice() to mimic the functionality of other PHP functions.

PHP

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

// Remove the last element of $input
array_pop($input);
array_splice($input, -1);

// Remove the first element of $input
array_shift($input);
array_splice($input, 0, 1);

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

// Replace the value in $input at index $x
$input[$x] = $y;
array_splice($input, $x, 1, $y);

As you can see, you can use array_splice() to append elements to an array, remove the last or first element of an array, insert an element at the beginning of an array and replace the value of input array at a given index. The last one only works in situations where the key is equal to offset.

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. One important thing that you should remember is that array_splice() splits an array into 2 arrays.

    PHP

    $input = array("red", "green", "blue", "yellow");
    
    print_r(array_splice($input, 3));
    /* Output —
    Array
    (
        [0] => yellow
    ) */
    
    print_r($input);
    /* Output —
    Array
    (
        [0] => red
        [1] => green
        [2] => blue
    ) */
    

    If you want to replace any array value, you can simply search the array index you want to replace and then use array_splice() with appropriate parameter values.

    PHP

    $input = array("red", "green", "blue", "yellow");
    $index = array_search('green', $input);
    array_splice($input, $index, 1, array('mygreeen'));
    
    print_r($input);
    /* Output —
    Array
    (
        [0] => red
        [1] => mygreeen
        [2] => blue
        [3] => yellow
    ) */
    

    Suggested by - mrsohailkhan at gmail dot com

Further Reading

  1. You can read more about the PHP array_splice() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top