-
-
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, -2, 1); // returns "d" - $ output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
- print_r(array_slice ($input, 2, -1));
- print_r(array_slice($input, 2, -1, true));
- ?>
-
Copy the code
The above example will output :
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
2.array_splice()
Carries three parameters, the same as above, and its function is to delete the subarray with length starting from offset.
Example:
-
-
- $input = array("red", "green", "blue", "yellow");
- array_splice($input, 2);
- // $input is now array("red", "green")
$input = array("red", "green", "blue", "yellow");
- array_splice($ input, 1, -1);
- // $input is now array("red", "yellow")
$input = array("red", "green", "blue ", "yellow");
- array_splice($input, 1, count($input), "orange");
- // $input is now array("red", "orange")
- < ;p>$input = array("red", "green", "blue", "yellow");
- array_splice($input, -1, 1, array("black", "maroon"));
- / / $input is now array("red", "green",
- // "blue", "black", "maroon")
$input = array("red", " green", "blue", "yellow");
- array_splice($input, 3, 0, "purple");
- // $input is now array("red", "green",
- // "blue" , "purple", "yellow");
- ?>
-
Copy code
|