[Solved] Calculate price based on interval [closed]


function calculatePrice($numberOfUnits) {
    // Initialise price
    $price = 0;

    // Prices: amount for category => price for category
    // Starts with largest category
    $prices = array(150 => 1.5, 100 => 2, 50 => 2.5, 0 => 3);

    // Loop over price categories
    foreach($prices as $categoryAmount => $categoryPrice) {

        // Calculate the numbers that fall into the category
        $amount = $numberOfUnits - $categoryAmount;

        // If units fall into the category, add to the price
        // and calculate remaining units
        if($amount > 0) {
            $price += $amount*$categoryPrice;
            $numberOfUnits -= $amount;
        }
    }

    // Return the total price
    return $price;
}

You can see it in action here.

solved Calculate price based on interval [closed]