split_array
Split an array into subarrays of a given length
Definition
array[array[any]] split_array(array[any] $array, int $countItemsInArray)
Splits an array into subarrays of $countItemsInArray
elements. The subarrays obtained in this way are returned enclosed in an array.
Parameters
array $array
Array to be split.
int $countItemsInArray
The length of subarrays to split into.
Returns
array
Array of subarrays of $array
of length $countItemsInArray
. Last element can be shorter if $countItemsInArray
doesn't divide the length of $array
evenly.
Examples
{
a: split_array(`[1, 2, 3, 4]`, `1`),
b: split_array(`[1, 2, 3, 4]`, `3`),
c: split_array(`[1, 2, 3, 4]`, `5`),
d: split_array(`[1, 2, 3, 4]`, `-1`), // Error
e: split_array(`[1, 2, 3, 4]`, `2.0`),
f: split_array(`[1, 2, 3, 4]`, `true`),
g: split_array(`[1, 2, 3, 4]`, `null`) // Error
}
{
"a":[[1],[2],[3],[4]],
"b":[[1,2,3],[4]],
"c":[[1,2,3,4]],
"e":[[1,2],[3,4]],
"f":[[1],[2],[3],[4]]
}
Updated about 1 year ago