-
$ Fruits = array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon ");
- $ Subset = array_slice ($ fruits, 3 );
- Print_r ($ subset );
-
// Output
- // Array ([0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon)
- ?>
-
Then, use the following negative length:
-
$ Fruits = array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon ");
- $ Subset = array_slice ($ fruits, 2,-2 );
- Print_r ($ subset );
-
// Output
- // Array ([0] => Orange [1] => Pear [2] => Grape)
- ?>
-
Join array array_splice ()
The array_splice () function deletes all elements in the array from offset to offset + length, and returns the deleted elements in an array. The format is array array_splice (array, int offset [, length [, array replacement]). when the offset is a positive value, the join starts from the offset position starting from the array, when offset is a negative value, the join starts from the offset position at the end of the array. If the optional length parameter is ignored, all elements from the offset position to the end of the array will be deleted. If length is given and it is a positive value, the join ends at the offset + leng th position starting with the array. Conversely, if length is given and it is a negative value, the combination ends at the position of count (input_array)-length starting with the array.
Example:
-
-
$ Fruits = array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon ");
- $ Subset = array_splice ($ fruits, 4 );
-
Print_r ($ fruits );
- Print_r ($ subset );
-
// Output
- // Array ([0] => Apple [1] => Banana [2] => Orange [3] => Pear)
- // Array ([0] => Grape [1] => Lemon [2] => Watermelon)
- ?>
-
You can use the optional parameter replacement to specify an array that replaces the target part.
Example:
-
-
$ Fruits = array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon ");
- $ Subset = array_splice ($ fruits, 2,-1, array ("Green Apple", "Red Apple "));
-
Print_r ($ fruits );
- Print_r ($ subset );
-
// Output
- // Array ([0] => Apple [1] => Banana [2] => Green Apple [3] => Red Apple [4] => Watermelon)
- // Array ([0] => Orange [1] => Pear [2] => Grape [3] => Lemon)
- ?>
|