note 126074 added to function.array-chunk
| From: | leon at valkenb dot org | Date: | Thu, 13 May 2021 02:24:00 +0000 |
| Subject: | note 126074 added to function.array-chunk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216378@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 114.23.214.163)
----
Manual Page -- https://php.net/manual/en/function.array-chunk.php
Edit -- https://main.php.net/note/edit/126074
Del: integrated -- https://main.php.net/note/delete/126074/integrated
Del: useless -- https://main.php.net/note/delete/126074/useless
Del: bad code -- https://main.php.net/note/delete/126074/bad+code
Del: spam -- https://main.php.net/note/delete/126074/spam
Del: non-english -- https://main.php.net/note/delete/126074/non-english
Del: in docs -- https://main.php.net/note/delete/126074/in+docs
Del: other reasons-- https://main.php.net/note/delete/126074
Reject -- https://main.php.net/note/reject/126074
Search -- https://main.php.net/manage/user-notes.php