array_splice()

data1928666221's avatar
Published Aug 30, 2023
Contribute to Docs

The array_splice() method takes an array as its input value and replaces elements within the array with new elements. The developer can specify within the method the starting index value for the replacement values, and the length of the values to be replaced.

Syntax

array_splice($input_array, $starting_index, $replacement_length, $replacement_values)

The array_splice() method has two required parameters and two optional parameters.

Required Parameters:

  • $input_array: The original array that will receive the new elements.
  • $starting_index: The index value that specifies where the replacement elements will start within the $input_array.

Note: Positive and negative index values are valid arguments for the $starting_index. A negative number counts from the last index value in the array (i.e. -3 equals third value from last).

Optional Parameters:

  • $replacement_length: The length of values to be replaced within the $input_array.
  • $replacement_values: An array that contains the new values that will be “spliced” into the $input_array.

Note: If there is only one replacement value a string can be used in place of an array.

Example

This example demonstrates a use of array_splice().

<?php
$colors_array = array("red", "orange", "yellow", "green", "blue", "purple");
$new_colors = array("dandelion yellow", "forest green");
array_splice($colors_array, 2, 2, $new_colors);
print_r($colors_array);
?>

This is the output of the previous example:

Array (
[0] => "red"
[1] => "orange"
[2] => "dandelion yellow"
[3] => "forest green"
[4] => "blue"
[5] => "purple"
)

Codebyte Example

This runnable codebyte example demonstrates an alteration using array_splice().

Code
Output
Loading...

All contributors

Contribute to Docs

Learn PHP on Codecademy