3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $vouchers = array( 1 => 100, 3 => 150, 5 => 100 ); $discounts = array(); $orders = array(50, 100, 200, 300, 500, 1000); foreach ($orders as $order) { $remainingValue = $order; foreach($vouchers as $id => $voucher) { $discount = max (0, min ($remainingValue, $voucher)); $discounts[$order][$id] = $discount; $remainingValue -= $discount; } $discounts[$order]['sold'] = $remainingValue; } var_dump($discounts);
based on 6BuqO
Output for 4.3.0 - 7.2.0
array(6) { [50]=> array(4) { [1]=> int(50) [3]=> int(0) [5]=> int(0) ["sold"]=> int(0) } [100]=> array(4) { [1]=> int(100) [3]=> int(0) [5]=> int(0) ["sold"]=> int(0) } [200]=> array(4) { [1]=> int(100) [3]=> int(100) [5]=> int(0) ["sold"]=> int(0) } [300]=> array(4) { [1]=> int(100) [3]=> int(150) [5]=> int(50) ["sold"]=> int(0) } [500]=> array(4) { [1]=> int(100) [3]=> int(150) [5]=> int(100) ["sold"]=> int(150) } [1000]=> array(4) { [1]=> int(100) [3]=> int(150) [5]=> int(100) ["sold"]=> int(650) } }