- var_dump: documentation ( source)
- min: documentation ( source)
- array_pop: documentation ( source)
<?php
$vouchers = array(
1 => 100,
3 => 150,
5 => 100
);
$discounts = array();
$orders = array(50, 100, 200, 300, 500, 1000);
foreach ($orders as $order) {
$vouchers2 = $vouchers;
foreach ($vouchers2 as $id => $voucher) {
$discounts[$order][$id] = 0;
}
$remainingValue = $order;
$voucher = array_pop($vouchers2);
if ($voucher === null) {
echo 'exiting for order '. $order . PHP_EOL;
continue;
}
while($remainingValue > 0 && $voucher !== null) {
$value = min ($remainingValue, $voucher);
$discounts[$order][$id] = $value;
$voucher = array_pop($vouchers2);
$remainingValue -= $value;
}
}
var_dump($discounts);