<?php
function chunkPreserveKeys(array $arr, int $size): array {
$chunks = [];
$i = 0;
foreach ($arr as $k => $v) {
$chunks[intdiv($i++, $size)][$k] = $v;
}
return $chunks;
}
$data = [10=>'a', 20=>'b', 30=>'c', 40=>'d', 50=>'e'];
print_r(chunkPreserveKeys($data, 2));
// [0 => [10=>a, 20=>b], 1 => [30=>c, 40=>d], 2 => [50=>e]]
Create a free account and build your private vault. Share publicly whenever you want.