fork download
  1. <?php
  2. $a = 300;
  3.  
  4. $b = array(
  5. 50,
  6. 570,
  7. 2400,
  8. );
  9.  
  10. $s = array_sum($b);
  11. if ($s > $a) {
  12. $ratio = 1 - $a / $s;
  13. for ($i = 0; $i < count($b); $i++) {
  14. $b[$i] *= $ratio;
  15. }
  16. }
  17.  
  18. var_dump( $b, $s, $ratio);
  19.  
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
array(3) {
  [0]=>
  float(45.033112582781)
  [1]=>
  float(513.37748344371)
  [2]=>
  float(2161.5894039735)
}
int(3020)
float(0.90066225165563)