3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [11, 3, 45, 6, 61, 89, 22]; function array_slimmer(array $array, int $finalCount): array { // no work to be done if we're asking for more than what the array holds // same goes if we're asking for just one array or (nonsensical) less if ($finalCount >= count($array) || $finalCount < 2) { return $array; } return array_map(function (array $chunk) { // rounded to two decimals, but you can modify to accomodate your needs return round(array_sum($chunk) / count($chunk), 2); }, custom_chunk($array, $finalCount)); } function custom_chunk($array, $maxrows) { $size = sizeof($array); $columns = ceil($size / $maxrows); $fullrows = $size - ($columns - 1) * $maxrows; for ($i = 0; $i < $maxrows; ++$i) { $result[] = array_splice($array, 0, ($i < $fullrows ? $columns : $columns - 1)); } return $result; } print_r(array_slimmer($array, 2)); print_r(array_slimmer($array, 3)); print_r(array_slimmer($array, 4));
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => 16.25 [1] => 57.33 ) Array ( [0] => 19.67 [1] => 33.5 [2] => 55.5 ) Array ( [0] => 7 [1] => 25.5 [2] => 75 [3] => 22 )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
32.45 ms | 405 KiB | 5 Q