Core\Arr::padStart
Core\Arr::padStart(array<T> $a, uint $size, T $value): array<T>Core\Arr::padStart( array<T> $a, uint $size, T $value, ): array<T>
Returns a new array with copies of a value added in front, until the array has the length you asked for.
Core\Arr::padStart never changes the array you give it. The result is always a list: its keys are numbers counting from "0", and the keys your array had are gone. The copies come first, then every value of your array.
An array that is already that long, or longer, is not padded. You get its values back as a list.
Good to know: the number you pass is the length of the whole result. It is not the number of copies to add.
Parameters
$aarray<T>The array to pad.$sizeuintThe length to reach; an array already that long is not padded.$valueTThe value of every added entry.
Return value
Returns array<T>. A list renumbered from "0". Every key is discarded, padded or not.
Errors
RuntimeErrorThe padded result is larger than the largest array this process can store.
Examples
Fill a row to a fixed length
<?nvs// Shows how to add values in front of an array until it has the length you// want.
array<string> $answers = ['yes', 'no'];
// `Core\Arr::padStart` adds copies of "-" in front until there are five// entries. This prints "- - - yes no".array<string> $row = Core\Arr::padStart($answers, 5, '-');echo Core\Str::join($row, ' '), "\n";
// Your own array is not changed, so this prints 2.echo Core\Arr::count($answers), "\n";- - - yes no2The keys of the result
<?nvs// Shows which keys the result has, and what happens when the array is already// long enough.
array<string> $named = ['a' => 'one', 'b' => 'two'];
// The array has two entries and you ask for four, so two copies are added.// The keys of the result are numbers. This prints "0 1 2 3".array<string> $padded = Core\Arr::padStart($named, 4, '');echo Core\Str::join(Core\Arr::keys($padded), ' '), "\n";
// The array already has two entries, so nothing is added. You get its values// back as a list. This prints "one two".array<string> $same = Core\Arr::padStart($named, 2, '');echo Core\Str::join($same, ' '), "\n";0 1 2 3one twoA chart of seven days
<?nvs// Shows how to prepare a chart of the last seven days for somebody who has// been here for three days. The missing days are added in front as 0, so the// newest day is always the last entry.
array<int> $visits = [4, 9, 2];
array<int> $week = Core\Arr::padStart($visits, 7, 0);
// This prints 0 four times, then 4, 9 and 2, each on its own line.foreach ($week as int $day) { echo $day, "\n";}0000492