Vai al contenuto

Extract a slice of the array in PHP:

Extract a slice of the array in PHP:
* https://www.php.net/manual/en/function.array-slice.php

PHP
1
2
3
4
5
6
7
array_slice(array $array, int $offset, ?int $length = null): array
$input = [ "a", "b", "c", "d", "e" ];
$output = array_slice($input, 2);     // => [ "c", "d", "e" ]
$output = array_slice($input, -2, 1); // => [ "d" ]
$output = array_slice($input, 0, 3);  // => [ "a", "b", "c" ]
$output = array_slice($input, 0, -2); // => [ "d", "e" ]
$output = array_slice($input, -1);    // => [ "e" ]