3v4l.org

run code in 300+ PHP versions simultaneously
<?php $vouchers = array( 1 => 100, 3 => 150, 5 => 100 ); $specials = array( 3 => true ); $discounts = array(); $orders = [50, 100, 200, 300, 500, 1000]; foreach ($orders as $order) { foreach ($vouchers as $voucher) { $discounts[$order][$voucher] = 0; } $remainingValue = $order; $voucher = array_pop($vouchers); if ($voucher === null) { continue; } while($remainingValue > 0 && $voucher !== null) { $value = min ($remainingValue, $voucher); $discounts[$order][$voucher] = $value; $vaucher = array_pop($vouchers); } } var_dump($discounts);
Output for 5.4.1 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 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.6

Process exited with code 137.

preferences:
225.7 ms | 404 KiB | 228 Q