Skip to content

Core\Arr::from

Core\Arr::from(
    array<T>|Iterable<T>|Iterator<T> $items,
    {limit?: uint},
): array<T>

Builds an array out of anything a foreach can walk.

The source can be an array, a generator, or an object that gives values one at a time. Core\Arr::from reads the source once, from the first value to the last, and returns a list. The result is numbered from 0, so the keys of a source array are not kept. A generator runs only as far as it is read.

The limit option stops the reading after that many values. This is what you use for a sequence that never ends on its own. Without a limit, such a sequence is read until the program reaches its memory limit and stops with an error. A limit of 0 reads nothing at all.

The examples below show a generator collected into an array, an array with keys turned into a plain list, and the first five values of a sequence that never ends.

Parameters

  • $itemsarray<T>|Iterable<T>|Iterator<T>The sequence to read. It is read once.

Options

  • limituintoptionalStops after this many elements. It is the only protection against a generator that never ends. By default every element is taken, and 0 takes nothing.

Return value

Returns array<T>. A list renumbered from "0". An iterator has no keys, and the keys of an array are discarded.

Examples

Collect the values of a generator

<?nvs
// Shows how to collect the values of a generator into an array. The generator
// gives one number at a time, and `Core\Arr::from` puts them all in a list.
class Fibonacci
{
// `yield` gives one number to whoever reads this generator. The method
// then waits there until the next number is wanted.
public static function first(uint $count): Iterator<int>
{
int $a = 1;
int $b = 1;
uint $i = 0;
while ($i < $count) {
yield $a;
int $next = $a + $b;
$a = $b;
$b = $next;
$i = $i + 1;
}
}
}
// This prints [1,1,2,3,5,8,13,21].
array<int> $numbers = Core\Arr::from(Fibonacci::first(8));
echo Core\Json::encode($numbers), "\n";
// The array is an ordinary one, so every `Core\Arr` method works on it.
echo Core\Arr::count($numbers), "\n";
Output
[1,1,2,3,5,8,13,21]
8

An array with keys becomes a list

<?nvs
// Shows how to turn an array that has keys into a plain list. The result of
// `Core\Arr::from` is always numbered from 0.
array<string> $byId = [17 => 'Ada', 42 => 'Grace', 91 => 'Alan'];
// The keys are dropped and the values keep their order.
// This prints ["Ada","Grace","Alan"].
array<string> $names = Core\Arr::from($byId);
echo Core\Json::encode($names), "\n";
// The array you started with is unchanged.
// This prints {"17":"Ada","42":"Grace","91":"Alan"}.
echo Core\Json::encode($byId), "\n";
Output
["Ada","Grace","Alan"]
{"17":"Ada","42":"Grace","91":"Alan"}

The next five invoice numbers

<?nvs
// Shows how to take the first few values of a sequence that never ends. A
// program reserves the next five invoice numbers this way. Without a limit
// the reading would never finish.
class Invoices
{
// This generator has no end. It gives one number after another for as
// long as somebody keeps reading it.
public static function numbers(uint $next): Iterator<string>
{
uint $n = $next;
while (true) {
yield 'INV-' . $n;
$n = $n + 1;
}
}
}
// `{ limit: 5 }` reads five values and then stops the generator.
// This prints ["INV-1007","INV-1008","INV-1009","INV-1010","INV-1011"].
array<string> $batch = Core\Arr::from(Invoices::numbers(1007), { limit: 5 });
echo Core\Json::encode($batch), "\n";
// A limit of 0 reads nothing. The generator is never started.
// This prints [].
echo Core\Json::encode(Core\Arr::from(Invoices::numbers(1007), { limit: 0 })), "\n";
Output
["INV-1007","INV-1008","INV-1009","INV-1010","INV-1011"]
[]