PHP array_splice() Function
In this chapter you will learn:
- Definition for PHP array_splice() Function
- Syntax for PHP array_splice() Function
- Parameter for PHP array_splice() Function
- Note for PHP array_splice() Function
- Example - Replace elements in an array
- Example - With the length parameter set to 0
- Example - inserts two new elements at the third position in the array
- Example - remove and insert elements at the same time
- Example - What happens if you omit the third argument
- Example - array_splice() automatically casts the fourth argument to an array before using it
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 j av a2 s . co 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/*j a v a 2s . 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// jav a 2 s .c o 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 // java 2 s . 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 //from jav a 2 s . com
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 java2 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 j a v a 2s . com*/
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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_sum() Function
- Syntax for PHP array_sum() Function
- Parameter for PHP array_sum() Function
- Example - Sum an indexed array
- Example - Sum values in an associate array