Core\Arr::append
Core\Arr::append(array<T> $a, T ...$values): array<T>Core\Arr::append( array<T> $a, T ...$values, ): array<T>
Adds one or more values to the end of an array.
Core\Arr::append never changes the array you give it. It returns a new array: first every entry of your array, under the key it already had, then each value you added.
Every key in Novis is a string. An added value gets the same key that $a[] = $v would give it. For a list of three entries, that is the key "3", then "4". An array with no number key at all starts at "0". The array remembers its next number key, and unset does not lower it. So when "9" is the largest number key, the next key after unset($a["9"]) is still "10".
You can pass as many values as you like in one call. They are added in the order you write them. If you pass no value, the result is your array, entry for entry.
Parameters
$aarray<T>The array to extend....$valuesTvariadicThe values to add, in written order.
Return value
Returns array<T>. First every entry of $a, with its own key. Then each value, with the key that $a[] = $v would give it. If an entry was removed from $a, the new key can be higher than the largest key $a has now. With no values, the result is the same as $a.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Arr::append uses the same key as $a[] = $v. After unset removes the largest number key, the next value gets the key after the removed one. |
Examples
Add one item to a list
<?nvs// Shows how to add one value to the end of a list.
array<string> $basket = ['bread', 'milk'];
// `Core\Arr::append` returns a new array. Your own array stays as it was.array<string> $bigger = Core\Arr::append($basket, 'apples');
// This prints bread, milk and apples, each on its own line.foreach ($bigger as string $item) { echo $item, "\n";}
// `$basket` still has two entries, so this prints 2.echo Core\Arr::count($basket), "\n";breadmilkapples2Add several values and see their keys
<?nvs// Shows how to add several values in one call, and which key each one gets.
array<int> $stock = ['apples' => 5, 'bread' => 2];
// The keys you already have are kept. An added value gets a number as its key.// These two keys are words, so the numbering starts at "0".array<int> $counted = Core\Arr::append($stock, 7, 9);
// This prints apples, bread, 0 and 1.foreach (Core\Arr::keys($counted) as string $key) { echo $key, "\n";}
// A list already has number keys. The largest here is "2", so the added value// gets "3" and this prints 3.array<int> $sizes = [38, 40, 42];echo Core\Arr::lastKey(Core\Arr::append($sizes, 44)), "\n";applesbread013Collect the problems with a form
<?nvs// Shows how a program collects the problems with a form before it answers.// Each check that fails adds one message to the list.
array<string> $form = ['name' => '', 'email' => 'kim.example.com'];array<string> $problems = [];
if ($form['name'] == '') { $problems = Core\Arr::append($problems, 'Please enter your name.');}
if (!Core\Str::contains($form['email'], '@')) { $problems = Core\Arr::append($problems, 'Please enter a valid email address.');}
// Both checks failed, so this prints both messages.foreach ($problems as string $problem) { echo $problem, "\n";}Please enter your name.Please enter a valid email address.