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.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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:
162.65 ms | 403 KiB | 153 Q