From eb869071b833631df1b1dc2d351014c29dbb45cc Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 16 Nov 2021 14:40:38 +0100 Subject: [PATCH] Add class for splitting a list into lists of a predefined size --- src/MD_STD.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/MD_STD.php b/src/MD_STD.php index b16e1ea..dc8ea31 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -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 + */ + 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; + + } }