Skip to content

Core\Arr::withoutFirst

Core\Arr::withoutFirst(array<T> $a): array<T>Core\Arr::withoutFirst(
    array<T> $a,
): array<T>

Returns a new array with every entry except the first one.

Core\Arr::withoutFirst never changes the array you give it. Every entry that stays keeps its own key. A list numbered from "0" gives a result numbered from "1". Use Core\Arr::values when you want the keys numbered from "0" again.

Core\Arr::first returns the value this method leaves out. Use the two together when you want to read the first value and then continue with the rest.

Good to know: an array with one entry gives an empty array, and an empty array gives an empty array. Both are normal results, and neither is an error.

Parameters

  • $aarray<T>The array to shorten.

Return value

Returns array<T>. The remaining entries under their own keys, never renumbered; the empty array for an array of one entry or none.

Examples

Everyone after the first

<?nvs
// Shows how to get every value of a list except the first one.
array<string> $queue = ['Ada', 'Ben', 'Cleo'];
// `Core\Arr::withoutFirst` returns a new array with the other two names.
array<string> $waiting = Core\Arr::withoutFirst($queue);
// This prints "Ben" and "Cleo".
foreach ($waiting as string $name) {
echo $name, "\n";
}
// Your own array is not changed, so this prints 3.
echo Core\Arr::count($queue), "\n";
Output
Ben
Cleo
3

The keys that stay

<?nvs
// Shows which keys the result has. Every entry that stays keeps its own key.
array<string> $columns = ['name', 'email', 'city'];
array<string> $rest = Core\Arr::withoutFirst($columns);
// The entry under the key 0 is gone. The other two keys stay as they were,
// so this prints 1 and 2.
foreach (Core\Arr::keys($rest) as string $key) {
echo $key, "\n";
}
// `Core\Arr::values` numbers the keys from 0 again. This prints 0 and 1.
foreach (Core\Arr::keys(Core\Arr::values($rest)) as string $key) {
echo $key, "\n";
}
Output
1
2
0
1

The arguments of a command

<?nvs
// Shows how to read a command somebody typed. The first word is the name of
// the command, and the words after it are its arguments.
string $line = 'copy report.pdf backup/';
array<string> $words = Core\Str::split($line, ' ');
// This prints "copy".
echo $words[0], "\n";
array<string> $arguments = Core\Arr::withoutFirst($words);
// This prints "report.pdf backup/".
echo Core\Str::join($arguments, ' '), "\n";
// This prints 2.
echo Core\Arr::count($arguments), "\n";
Output
copy
report.pdf backup/
2