array_splice
array array_splice(array array, int offset, [int length], [array replacement])
|
array
|
Array to act on
|
|
offset
|
Starting element index
|
|
length
|
Length of segment to replace
|
|
replacement
|
Array of replacement elements
|
Replaces part of an array with another array.
Returns:
Array of elements deleted from the original array; NULL on failure
Description:
This function deletes the elements of array
starting at the position given by offset
. If offset
is positive, it gives the number of elements from the beginning of array
from which to start deleting; if negative, it gives the number of elements from the end at which to start deleting.
array
is altered in place, and an array containing the deleted elements is returned.
If length
is given and is positive, it gives the number of elements to delete. If negative, it gives the number of elements from the end of array
at which to stop deleting. If not given, all elements from offset
to the end of array
are deleted.
If replacement
is given, the deleted elements of array
are replaced with the contents of replacement
. You can use this to cause replacement
to simply be inserted into array
with no deletions taking place by setting length
to 0.
Version:
PHP 4
Example:
Splice arrays together
$arr1 = array("black", "white", "eggplant", "grey");
$arr2 = array("red", "blue", "green");
echo "Original arrays: ", implode(", ", $arr1), " & ", implode(", ", $arr2),
"\n";
$items = array_splice($arr1, 1, 2, $arr2);
echo "New array: ", implode(", ", $arr1), "\n";
echo "Parts removed: ", implode(", ", $items);
Output:
Original arrays: black, white, eggplant, grey & red, blue, green
New array: black, red, blue, green, grey
Parts removed: white, eggplant
|