3v4l.org

run code in 300+ PHP versions simultaneously
<?php $percent = [20.88, 14.93, 14.14, 13.29, 5.06, 4.43, 4.24, 4.22, 2.57, 2.51, 2.38, 2.18, 1.94, 1.80, 1.34, 1.21, 0.81, 0.63, 0.50, 0.48, 0.30, 0.16]; $limit = 6; if ($limit * count($percent) < 100) die("Not possible"); $limited = array_map(function($value) use ($limit) { return min($value, $limit); }, $percent); while (($remain = 100 - array_sum($limited)) > 0) { $filtered = array_filter($limited, function($v) use ($limit) { return $v < $limit; }); $evenly = $remain / count($filtered); $limited = array_map(function($value) use ($evenly, $limit) { return min($value + $evenly, $limit); }, $limited); } print_r($limited); print_r(array_sum($limited));
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.27, 8.0.0 - 8.0.15, 8.1.0 - 8.1.2
Array ( [0] => 6 [1] => 6 [2] => 6 [3] => 6 [4] => 6 [5] => 6 [6] => 6 [7] => 6 [8] => 4.9407142857143 [9] => 4.8807142857143 [10] => 4.7507142857143 [11] => 4.5507142857143 [12] => 4.3107142857143 [13] => 4.1707142857143 [14] => 3.7107142857143 [15] => 3.5807142857143 [16] => 3.1807142857143 [17] => 3.0007142857143 [18] => 2.8707142857143 [19] => 2.8507142857143 [20] => 2.6707142857143 [21] => 2.5307142857143 ) 100

preferences:
27.22 ms | 461 KiB | 5 Q