Add class for splitting a list into lists of a predefined size

This commit is contained in:
Joshua Ramon Enslin 2021-11-16 14:40:38 +01:00
parent e19e0c875c
commit eb869071b8
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -763,4 +763,32 @@ final class MD_STD {
}
}
/**
* Splits an lists of integers into parts of a predefined size and returns those
* as sub-lists of a superordinate list.
*
* @param integer[] $input Input array.
* @param integer $size Size of each part of the return set.
*
* @return array<integer[]>
*/
public static function split_int_array_into_sized_parts(array $input, int $size):array {
if ($size < 1) {
throw new Exception("Size must be lower than 1");
}
$output = [];
$max = count($input);
$offset = 0;
while ($offset < $max) {
$output[] = array_slice($input, $offset, $size - 1);
$offset += $size;
}
return $output;
}
}