3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr = [6,4,9,3,8,2,1,5]; // Calculate the total of the array elements $sum = array_sum($arr); $len = count($arr); // Using integer division and modulo arithmetic calculate the average and remainder $avg = intdiv($sum,$len); $rem = $sum % $len; // Create a result array and fill with the average $result = array_fill(0,$len,$avg); // Spread the remainder over the first few elements. while($rem){$result[--$rem]++;} var_dump($result); ?>

preferences:
89.35 ms | 1277 KiB | 5 Q