3v4l.org

run code in 300+ PHP versions simultaneously
<?php $vouchers = array( 1 => 100, 3 => 150, 5 => 100 ); $discounts = array(); $orders = array(50, 100, 200, 300, 500, 1000); foreach ($orders as $order) { echo "order: " . $order . PHP_EOL; $remainingValue = $order; foreach($vouchers as $id => $voucher) { echo "voucher : " . $voucher . PHP_EOL; $discounts[$order][$id] = 0; $discount = max (0, min ($remainingValue, $voucher)); $remainingValue -= $discount; } } var_dump($discounts);

preferences:
49.84 ms | 402 KiB | 5 Q