Skip to content

Core\Arr::slice

Core\Arr::slice(
    array<T> $a,
    int $offset,
    ?int $length = null,
    {preserveKeys?: bool},
): array<T>

Gives you the entries of one window of an array.

A position counts entries from the front and starts at 0, whatever the keys are. $offset is where the window opens, and $length is how many entries it holds. A negative offset counts back from the last entry. A negative length stops that many entries short of the end. A null length runs to the end of the array.

The window never reaches outside the array. An offset before the first entry opens at the first entry. A window that starts after the last entry holds nothing, so you get an empty array.

The result is a new array, and the original is not changed. The entries of the result are numbered from 0, and the keys of the original are gone. Set preserveKeys to keep them.

Parameters

  • $aarray<T>The array to take from.
  • $offsetintThe position where the range starts. A negative offset counts back from the last entry. An offset before the first entry starts at the first entry.
  • $length?intdefault nullHow many entries the range has. A negative length stops that many entries before the end, and null takes every entry to the end.

Options

  • preserveKeysbooloptionalKeeps every entry's own key. By default every key is discarded, string keys included, and the result is renumbered from "0".

Return value

Returns array<T>. The entries in the range; the empty array when the range ends before it starts.

Examples

A window of entries

<?nvs
// Shows how to take one window of entries out of an array, and which keys the
// window carries.
array<string> $days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
// The window opens at position 2 and holds 2 entries. A position counts
// entries from the front and starts at 0. This prints ["Wed","Thu"].
echo Core\Json::encode(Core\Arr::slice($days, 2, 2)), "\n";
// A `null` length runs to the end of the array. This prints ["Thu","Fri"].
echo Core\Json::encode(Core\Arr::slice($days, 3, null)), "\n";
array<int> $stock = ['apples' => 12, 'pears' => 4, 'plums' => 7];
// The result is numbered from 0, and the keys of the original are gone. This
// prints [4,7].
echo Core\Json::encode(Core\Arr::slice($stock, 1, null)), "\n";
// `preserveKeys` keeps every value under its own key. This prints
// {"pears":4,"plums":7}.
echo Core\Json::encode(Core\Arr::slice($stock, 1, null, { preserveKeys: true })), "\n";
Output
["Wed","Thu"]
["Thu","Fri"]
[4,7]
{"pears":4,"plums":7}

The ends of the window

<?nvs
// Shows where the window opens and closes when a position is negative or lies
// outside the array.
array<string> $days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
// A negative offset counts back from the last entry. This prints
// ["Thu","Fri"].
echo Core\Json::encode(Core\Arr::slice($days, -2, null)), "\n";
// A negative length stops that many entries short of the end. This prints
// ["Mon","Tue","Wed"].
echo Core\Json::encode(Core\Arr::slice($days, 0, -2)), "\n";
// An offset before the first entry opens at the first entry. This prints
// ["Mon","Tue"].
echo Core\Json::encode(Core\Arr::slice($days, -99, 2)), "\n";
// A window that starts after the last entry holds nothing. This prints [].
echo Core\Json::encode(Core\Arr::slice($days, 99, 2)), "\n";
Output
["Thu","Fri"]
["Mon","Tue","Wed"]
["Mon","Tue"]
[]

One page of results

<?nvs
// Shows one page of search results. The page number and the page size decide
// where the window opens, which is how a long list is shown a page at a time.
array<string> $found = [
'Keyboard',
'Lamp',
'Monitor',
'Mouse',
'Notebook',
'Pen',
'Printer',
];
int $perPage = 3;
int $page = 2;
// The second page opens at position 3. This prints `Mouse, Notebook, Pen`.
array<string> $shown = Core\Arr::slice($found, ($page - 1) * $perPage, $perPage);
echo 'page ', $page, ': ', Core\Str::join($shown, ', '), "\n";
// The last page holds what is left, which can be less than the page size.
// This prints `Printer`.
array<string> $rest = Core\Arr::slice($found, 2 * $perPage, $perPage);
echo 'page 3: ', Core\Str::join($rest, ', '), "\n";
Output
page 2: Mouse, Notebook, Pen
page 3: Printer