<?php
function custom_chunk($array, $maxrows) {
$result = [];
$size = sizeof($array);
$columns = ceil($size / $maxrows);
$fullrows = $size - ($columns - 1) * $maxrows;
for ($i = 0; $i < $maxrows; ++$i) {
$result[] = implode(', ', array_splice($array, 0, ($i < $fullrows ? $columns : $columns - 1)));
}
return $result;
}
$data = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7'];
var_export(custom_chunk($data, 3));
- Output for 5.6.38, 7.1.0 - 7.1.23, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array (
0 => 'Item 1, Item 2, Item 3',
1 => 'Item 4, Item 5',
2 => 'Item 6, Item 7',
)
preferences:
102.24 ms | 408 KiB | 5 Q