Voting

: max(six, five)?
(Example: nine)

The Note You're Voting On

normiridium at gmail dot com
9 years ago
A breakdown by groups with excess:

function array_chunk_greedy($arr, $count){
$arr = array_chunk($arr, $count);
if(($k = count($arr)-1) > 0){
if(count($arr[$k]) < $count){
$arr[$k-1] = array_merge($arr[$k-1], $arr[$k]);
unset($arr[$k]);
}
}
return $arr;
}

$arr = range(1, 13);
$arr = array_chunk_greedy($arr, 4);

print_r($arr);

[1,2,3,4,5,6,7,8,9,10,11,12,13] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13]

More examples:

[1,2,3,4,5,6,7,8,9,10,11,12] —> [1,2,3,4] [5,6,7,8] [9,10,11,12]
[1,2,3,4,5,6,7,8,9,10,11,12,13] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13,14]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13,14,15]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] —> [1,2,3,4] [5,6,7,8] [9,10,11,12] [13,14,15,16]

Example report:

$arr = range(1, 45);
$arr = array_chunk_lazy($arr, 10);

$arr = array_map(function($sub_value) {
return implode('<br>', $sub_value);
}, $arr);

$title = '<h2>title</h2>';
$arr = $title.implode($title, $arr).$title;

echo $arr;

<< Back to user notes page

To Top