0

我有一个带有嵌套数组的数组,如下所示。

cart ( 
[total] => 98 
[itemcount] => 3 
[items] => Array ( [0] => 0 [1] => 3 [2] => 5 ) 
[itemprices] => Array ( [0] => 33.00 [3] => 32.00 [5] => 33 ) 
[itemqtys] => Array ( [0] => 1 [3] => 1 [5] => 1 ) 
[iteminfo] => Array ( [0] => Chemistry [3] => Additional Mathematics [5] => Physics ) 
)

当用户输入正确的折扣代码时,我在开关中有下面的代码,此功能将触发。我可以让它显示折扣价。

$anewvalue = 16.50;
$physubject = "Physics";
$index = array_search($physubject , $cart->iteminfo);
if (false !== $index) {
$cart->itemprices[$index] = $anewvalue;}

问题是,每当用户输入正确的折扣代码并提交表单时,我如何更新总价,以便总价始终是最新的,因为现在它不会这样做。

4

1 回答 1

0

每次应用正确的折扣时,您都需要重新计算总数。

if (false !== $index) {
    $cart->itemprices[$index] = $anewvalue;
    //re-calculate the total
    $total = 0;
    foreach ($cart->itemprices as $key=>$itemprice) {
        $total += $itemprice * $cart->itemqtys[$key];
    }
    $cart->total = $total
}
于 2013-09-24T06:10:00.963 回答