forked from muglug/psl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice.php
38 lines (34 loc) · 875 Bytes
/
slice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
declare(strict_types=1);
namespace Psl\Arr;
use Psl;
use Psl\Dict;
/**
* Takes a slice from an array.
*
* Examples:
*
* Arr\slice([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], 5)
* => Arr(0, 1, 2, 3, 4, 5)
*
* Arr\slice([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], 5, 3)
* => Arr(0, 1, 2)
*
* @template Tk of array-key
* @template Tv
*
* @param array<Tk,Tv> $array Array to take the slice from
* @param int $start Start offset
* @param int $length Length (if not specified all remaining values from the array are used)
*
* @throws Psl\Exception\InvariantViolationException If the $start offset or $length are negative
*
* @return array<Tk, Tv>
*
* @deprecated use `Dict\slice()` instead.
* @see Dict\slice()
*/
function slice(array $array, int $start, ?int $length = null): array
{
return Dict\slice($array, $start, $length);
}