Core\Arr::chunk
Gives you the entries of an array in runs of the same size.
$size is how many entries each run holds, and the result is a list of those runs. The last run is short when the number of entries does not divide by the size. An array with no entries gives you no runs at all. A $size of 0 throws a RuntimeError, because no run length would ever reach the end of the array.
Every run is numbered from 0, and the keys of the original are gone. Set preserveKeys to keep every entry under its own key inside its run. The list of runs itself is always numbered from 0, because a run has no key of its own.
The result is a new array, and the original is not changed.
Parameters
$aarray<T>The array to split.$sizeuintHow many entries each chunk has; at least1.
Options
preserveKeysbooloptionalKeeps each entry's own key inside its chunk. By default every chunk is renumbered from"0".
Return value
Returns array<array<T>>. A list of chunks. The empty array gives no chunks.
Errors
RuntimeError$sizeis0.
Examples
Runs of the same size
<?nvs// Shows how to split an array into runs of the same size.
array<string> $days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
// Each run holds 2 entries. Five entries do not divide by two, so the last run// is short. This prints [["Mon","Tue"],["Wed","Thu"],["Fri"]].echo Core\Json::encode(Core\Arr::chunk($days, 2)), "\n";
// A size larger than the array gives one run with everything in it. This// prints [["Mon","Tue","Wed","Thu","Fri"]].echo Core\Json::encode(Core\Arr::chunk($days, 99)), "\n";
// An array with no entries gives no runs at all. This prints [].array<string> $nothing = [];echo Core\Json::encode(Core\Arr::chunk($nothing, 2)), "\n";[["Mon","Tue"],["Wed","Thu"],["Fri"]][["Mon","Tue","Wed","Thu","Fri"]][]The keys inside a run
<?nvs// Shows which keys the runs carry, and what happens when the size is 0.
array<int> $stock = ['apples' => 12, 'pears' => 4, 'plums' => 7];
// Every run is numbered from 0, and the keys of the original are gone. This// prints [[12,4],[7]].echo Core\Json::encode(Core\Arr::chunk($stock, 2)), "\n";
// `preserveKeys` keeps every entry under its own key inside its run. This// prints [{"apples":12,"pears":4},{"plums":7}].echo Core\Json::encode(Core\Arr::chunk($stock, 2, { preserveKeys: true })), "\n";
// A size of 0 throws a `RuntimeError`. You can catch it and read its message.try { array<array<int>> $runs = Core\Arr::chunk($stock, 0); echo Core\Json::encode($runs), "\n";} catch (RuntimeError $error) { echo 'the size was wrong: ', $error->message, "\n";}[[12,4],[7]][{"apples":12,"pears":4},{"plums":7}]the size was wrong: Core\Arr::chunk(): the `size` argument must be greater than 0, got 0Invitations in batches
<?nvs// Sends invitations in batches. A mail server limits how many addresses one// message may carry, so a long guest list is sent a batch at a time.
array<string> $guests = [ 'ada@example.com', 'grace@example.com', 'alan@example.com', 'ida@example.com', 'ina@example.com',];
// Each batch holds at most 2 addresses, and the last batch holds what is left.array<array<string>> $batches = Core\Arr::chunk($guests, 2);
int $number = 1;foreach ($batches as array<string> $batch) { echo 'batch ', $number, ': ', Core\Str::join($batch, ', '), "\n"; $number = $number + 1;}batch 1: ada@example.com, grace@example.combatch 2: alan@example.com, ida@example.combatch 3: ina@example.com