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 7.2.0 - 7.2.33, 7.3.0 - 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.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
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 )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
182.52 ms | 407 KiB | 5 Q