3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = [ 101 => 5, 109 => 100, 220 => 50, 231 => 45, 245 => 90 ]; $b = [ 'PRO' => 12, 'LOG' => 15, 'DEV' => 100 ]; $difference = 0; foreach ($a as &$minuend) { while ($minuend && ($b || $difference < 0)) { $suprahend = $difference < 0 ? -$difference : array_shift($b); $difference = $minuend - $suprahend; $minuend = max(0, $difference); } } var_export($a);

preferences:
26.67 ms | 406 KiB | 5 Q