Voting

: max(eight, four)?
(Example: nine)

The Note You're Voting On

leon at valkenb dot org
3 years ago
With no luck finding a function; here is the one I wrote:
It will evenly distribute items into a fixed amount of groups but also keeps items that were sorted close together to be in the same output groups.
<?php
function distributed_array_chunk(array $items, int $groups, bool $preserveKeys = false) {

$grouped = [];
$groupsPerItem = $groups / count($items);
$progress = 0.00;

foreach (
$items as $key => $value) {

$index = floor($progress += $groupsPerItem);

if (
$preserveKeys) {
$grouped[$index][$key] = $value;
}
else{
$grouped[$index][] = $value;
}
}

return
$grouped;
}
?>

<< Back to user notes page

To Top