From e2ada291f7c178fd867d87b27a65acbb1b79f991 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 14 Dec 2021 03:16:11 +0100 Subject: [PATCH] Fix bug in splitting int arrays, add function for splitting string arrays --- src/MD_STD.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/MD_STD.php b/src/MD_STD.php index afee1d4..1c3f9c8 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -773,7 +773,7 @@ final class MD_STD { * @param array $input Input array. * @param positive-int $size Size of each part of the return set. * - * @return array> + * @return array> */ public static function split_int_array_into_sized_parts(array $input, int $size):array { @@ -788,7 +788,36 @@ final class MD_STD { while ($offset < $max) { $cur = array_slice($input, $offset, $size - 1); if (!empty($cur)) $output[] = $cur; - $offset += $size; + $offset += $size - 1; + } + + return $output; + + } + + /** + * Splits an lists of strings into parts of a predefined size and returns those + * as sub-lists of a superordinate list. + * + * @param array $input Input array. + * @param positive-int $size Size of each part of the return set. + * + * @return array> + */ + public static function split_string_array_into_sized_parts(array $input, int $size):array { + + /* + if ($size < 1) throw new Exception("Size of the target array must be a positive integer"); + */ + + $output = []; + + $max = count($input); + $offset = 0; + while ($offset < $max) { + $cur = array_slice($input, $offset, $size - 1); + if (!empty($cur)) $output[] = $cur; + $offset += $size - 1; } return $output;