-
$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)
- ?>
-
Copy Code
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)
- ?>
-
Copy Code
Junction Array Array_splice ()
The Array_splice () function deletes all elements in the array from offset to end of Offset+length and returns the deleted element as an array. When the form is: array array_splice (array array, int Offset[,length[,array replacement]) offset is positive, the join starts at offset from the beginning of the array, When offset is negative, the join starts from the offset position at the end of the array. If the optional length parameter is omitted, all elements from the offset position to the end of the array are deleted. If length is given and positive, the join ends at offset + Leng th at the beginning of the array. Conversely, if length is given and is negative, the union ends at the position of Count (Input_array)-length from the beginning of 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)
- ?>
-
Copy Code
You can use the optional parameter replacement to specify an array that supersedes 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)
- ?>
Copy Code |