使用foreach增加变量

时间:2013-12-12 13:56:11

标签: php foreach increment

我想用foreach循环增加变量。我在会话中保存数据,并为每个数据块创建一个if语句来检查维度并将其连接到价格。然后我需要在变量$ pr_total_str_saving中增加这些价格。

有人可以帮我这个吗?

if(isset($_SESSION['straight_saving'])) : 

    foreach($_SESSION['straight_saving'] as $key => $val) {

        if($val['wz_saving_a'] >= 1 && $val['wz_saving_a'] <= 300) :
            $str_saving_price = 25;
        elseif($val['wz_saving_a'] >= 301 && $val['wz_saving_a'] <= 500) :
            $str_saving_price = 39;
        endif;

        $pr_total_str_saving + $str_saving_price;

    }

    echo $pr_total_str_saving;

endif;

2 个答案:

答案 0 :(得分:0)

if(isset($_SESSION['straight_saving'])) : 

$pr_total_str_saving = 0;

foreach($_SESSION['straight_saving'] as $key => $val) {

    if($val['wz_saving_a'] >= 1 && $val['wz_saving_a'] <= 300) :
        $str_saving_price = 25;
    elseif($val['wz_saving_a'] >= 301 && $val['wz_saving_a'] <= 500) :
        $str_saving_price = 39;
    endif;

    $pr_total_str_saving += $str_saving_price;

}

echo $pr_total_str_saving;

endif;

答案 1 :(得分:0)

我相信您正在寻找+=运营商。以下是如何使用它:

$pr_total_str_saving += $str_saving_price;