3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $precision = 100000; $offset = 0; $prizes = [ ['name' => '1', 'chance' => '0.1'], ['name' => '2', 'chance' => '0.2'], ['name' => '3', 'chance' => '0.3'], ['name' => '4', 'chance' => '0.1'], ]; $reduce = $offset; foreach ($prizes as $index => $prize) { $prizes[$index]['min'] = $reduce; $reduce += $prize['chance'] * $precision; $prizes[$index]['max'] = $reduce; } var_dump($prizes);
based on 3icnS
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.2.6
array(4) { [0]=> array(4) { ["name"]=> string(1) "1" ["chance"]=> string(3) "0.1" ["min"]=> int(0) ["max"]=> float(10000) } [1]=> array(4) { ["name"]=> string(1) "2" ["chance"]=> string(3) "0.2" ["min"]=> float(10000) ["max"]=> float(30000) } [2]=> array(4) { ["name"]=> string(1) "3" ["chance"]=> string(3) "0.3" ["min"]=> float(30000) ["max"]=> float(60000) } [3]=> array(4) { ["name"]=> string(1) "4" ["chance"]=> string(3) "0.1" ["min"]=> float(60000) ["max"]=> float(70000) } }