
$discounts = array(
1000 => 5,
1500 => 10,
2000 => 15,
);
function getDiscountPercent($sum, $discounts) {
for ($discount_percent = 0, reset($discounts); current($discounts) && $sum >= key($discounts); next($discounts)) {
$discount_percent = current($discounts);
}
return $discount_percent;
}
<?php
$discounts = array(
0 => 0,
10000 => 10,
25000 => 50
);
$price = 30000;
$priceRemain = $price;
$discountTotal = 0;
foreach (array_reverse($discounts, true) as $threshold => $discount) {
if($priceRemain >= $threshold) {
$discountTotal += ($priceRemain - $threshold) * $discount / 100;
$priceRemain = $threshold;
}
}
echo $price - $discountTotal;
public function getDiscountPercent(){
$count = count($this->discounts_stops);
$i = 0;
$sum = $this->sumProducts();
while ($count > $i){
if ((int)$sum < (int)$this->discounts_stops[$i]){
if ($i > 0){
return $this->discounts[$i-1];
}else{
return 0;
}
}
$i++;
}
return $this->discounts[$count-1];
}