Fix bug in splitting int arrays, add function for splitting string

arrays
This commit is contained in:
Joshua Ramon Enslin 2021-12-14 03:16:11 +01:00
parent aa7a3c5012
commit e2ada291f7
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -773,7 +773,7 @@ final class MD_STD {
* @param array<int, int> $input Input array. * @param array<int, int> $input Input array.
* @param positive-int $size Size of each part of the return set. * @param positive-int $size Size of each part of the return set.
* *
* @return array<non-empty-array<integer>> * @return array<non-empty-array<int, integer>>
*/ */
public static function split_int_array_into_sized_parts(array $input, int $size):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) { while ($offset < $max) {
$cur = array_slice($input, $offset, $size - 1); $cur = array_slice($input, $offset, $size - 1);
if (!empty($cur)) $output[] = $cur; 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<int, string> $input Input array.
* @param positive-int $size Size of each part of the return set.
*
* @return array<non-empty-array<int, string>>
*/
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; return $output;