duanquanhan2333 2013-06-20 14:47
浏览 12
已采纳

从循环中的多个项目中扣除25%

I wonder if someone could please help me out a little. I have a loop that loops through the contents of a shopping cart. I wish to apply a 25% discount to additional items purchased. So basically 1st item is full price and every other item is reduced by 25%. I've tried various methods but all i seem to get is the discount apply to all or nothing.

The loop below works perfectly if i remove the if statement and its contents thus not wishing to apply a discount. As it currently stands it does not add a discount at all. If i remove the if condition and use it's contents then it will apply a 25% discount to all items.

for($Loop = 0; $Loop < count($Cart); $Loop++)
{
    $Total += $ShoppingCart[$Loop][Price];

    if($Loop > 1) {
        $Total += $ShoppingCart[$Loop][Price];
    $PercentageAmount = 25;
    $TotalPrice = $TotalPrice * ((100-$PercentageAmount) / 100);    
    }
}

Edited:

Unfortunately none of the answers, although maybe technically good, do not fix my problem. I have had to result to placing 2 if statements within a loop and then calculating their combined total. Not an ideal solution but works perfectly never the less. Somehow i need the sort it so then the most expensive item is at full price. It would be much easier if i was not tied to using a loop in this fashion and instead could use array functions.

$i = 0; 
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
    if($i == 0) {
        $Total += $ShoppingCart[$Loop][Price];
    }                               
    if($i > 0) {
        $TotalMulti += $ShoppingCart[$Loop][Price];
        $TotalMulti = $TotalMulti * .75;    
    }
    $i++;
}
$NewTotal = $Total + $TotalMulti;
  • 写回答

3条回答 默认 最新

  • dseigqk7443 2013-06-20 14:50
    关注

    Here's how I would do it:

    $prices = array_column($ShoppingCart, 'Price');
    array_walk($prices, function(&$price, $i) { if($i) $price *= .75; });
    $total = array_sum($prices);
    

    How it works:

    1. The prices are pulled out in their own array -- I much prefer this because the discount code does not mess with the "normal" prices, which might cause unexpected complications.
    2. The array of prices is iterated over, and every element but the first is set to 75% of its value.
    3. The total price is just the sum of the discounted prices.

    This code depends on array_column, which is only available starting with PHP 5.5. For earlier versions you can either grab an implementation from here or substitute this:

    $prices = array_map(function($el) { return $el['Price']; }, $ShoppingCart);
    

    If the discount percentage is a variable you will also need this modification:

    $discount = .25;
    array_walk(
        $prices,
        function(&$price, $i) use($discount) { if($i) $price *= (1 - $discount); }
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源