PHP array_splice() Function
Definition
array_splice() function is the array equivalent of the string - manipulation function substr_replace().
The array_splice() function removes a range of elements and replaces them with new elements.
Both the removal and the replacement are optional, meaning you can just remove elements without adding new ones, or just insert new elements without removing any.
The function returns an array with the removed elements.
Syntax
PHP array_splice() Function has the following syntax.
array_splice(array,start,length,array)
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Array to splice |
start | Required. | start position. 0 = the first element. A negative number means to start from the last element. -2 means start at the second last element of the array. |
length | Optional. | how many elements to remove, and length of the returned array. For a negative number, the function will stop that far from the last element. If not set, the function will remove all elements from start-parameter. |
array | Optional. | Specifies an array to be inserted to the original array. If it's only one element, it can be a string. |
Note
When inserting an array, the keys of the inserted elements aren't preserved. They're reindexed using numeric keys. So array_splice() isn't that useful for inserting associative arrays. For example:
<?PHP//from w w w . j a v a2 s . c o m
$authors = array( "Java", "PHP", "CSS" );
array_splice( $authors, 1, 0, array( "authorName" => "MySQL" ) );
print_r( $authors );
?>
Notice how the "MySQL" element has had its original key ( "authorName") replaced with a numeric key (1).
The code above generates the following result.
Example 1
Replace elements in an array
<?php// w ww . j ava2 s .co m
$a1=array("a"=>"A","b"=>"B","c"=>"C","d"=>"java2s.com");
$a2=array("a"=>"A","b"=>"B");
array_splice($a1,0,2,$a2);
print_r($a1);
?>
The code above generates the following result.
Example 2
With the length parameter set to 0:
<?php//from w w w. ja v a2 s .co m
$a1=array("0"=>"Java","1"=>"java2s.com");
$a2=array("0"=>"PHP","1"=>"CSS");
array_splice($a1,1,0,$a2);
print_r($a1);
?>
The code above generates the following result.
Example 3
Adding two new elements to the middle
<?php // w w w .ja va2s . c o m
$authors = array( "Java", "PHP", "CSS" );
$arrayToAdd = array( "Python", "Javascript" );
print_r( $authors );
print_r( array_splice( $authors, 2, 0, $arrayToAdd ) );
print_r( $arrayToAdd );
print_r( $authors );
?>
The code above generates the following result.
This example inserts two new elements at the third position in the array, displaying the removed elements, which in this case is an empty array because no elements were removed:
print_r( array_splice( $authors, 2, 0, $arrayToAdd ) );
You can read this line as: "At the third position (2), remove zero (0) elements, then insert $arrayToAdd".
Example 4
Remove and insert elements at the same time:
print_r( array_splice( $authors, 0, 2, $arrayToAdd ) );
This code removes two elements from the start of the array (position 0), then inserts the contents of $arrayToAdd at position 0.
<?php //w w w .ja v a 2 s . c o m
echo "2. Replacing two elements with a new element";
$authors = array( "Java", "PHP", "CSS" );
$arrayToAdd = array( "Oracle" );
print_r( $authors );
print_r( array_splice( $authors, 0, 2, $arrayToAdd ) );
print_r( $arrayToAdd );
print_r( $authors );
?>
The code above generates the following result.
Example 5
What happens if you omit the third argument:
print_r( array_splice( $authors, 1 ) );
This code removes all the elements from the second position in the array (position 1) to the end of the array.
<?php /*from w w w .j a va 2 s . c om*/
echo "3. Removing the last two elements";
$authors = array( "Java", "PHP", "CSS" );
print_r( $authors );
print_r( array_splice( $authors, 1 ) );
print_r( $authors );
?>
The code above generates the following result.
Example 6
You don't have to pass an array as the fourth argument. If you only have one element to add, just pass the value.
array_splice() automatically casts the fourth argument to an array before using it.
print_r( array_splice( $authors, 1, 0, "Ruby" ) );
<?php /*from w w w. j av a 2 s. c o m*/
echo "4. Inserting a string instead of an array";
$authors = array( "Java", "PHP", "CSS" );
print_r( $authors );
print_r( array_splice( $authors, 1, 0, "Ruby" ) );
print_r( $authors );
?>
The code above generates the following result.