php总结了数组中的特定值

时间:2014-02-20 07:40:46

标签: php arrays foreach

foreach ($retrievependingorders as $pendingorders){
        $orderno = $pendingorders['order_no'];
        $lastupdate = $pendingorders['order_update_date'];
        $orderstatus = $pendingorders['order_status'];
        $totalprice = $pendingorders['total_price'];

        echo "<tr><td>$orderno</td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>";
}

伙计们,我有一个foreach循环,将所有数据库行回显到表中。我可以知道如何总结pendingorders数组中存在的所有'totalprice'吗?

4 个答案:

答案 0 :(得分:0)

试试这个:

$total = 0;
foreach ($retrievependingorders as $pendingorders){
        $orderno       = $pendingorders['order_no'];
        $lastupdate    = $pendingorders['order_update_date'];
        $orderstatus   = $pendingorders['order_status'];
        $totalprice    = $pendingorders['total_price'];
        $total        += $totalprice;

        echo "<tr><td>$orderno</td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>";
}

echo $total;

答案 1 :(得分:0)

这是你可以做的,采用var外部循环并将所有$totalprice添加到其中!!

$grand_total = 0 ;

foreach ($retrievependingorders as $pendingorders){
        $orderno = $pendingorders['order_no'];
        $lastupdate = $pendingorders['order_update_date'];
        $orderstatus = $pendingorders['order_status'];
        $totalprice = $pendingorders['total_price'];
        $grand_total += $totalprice ; // Here we are summing the values
        echo "<tr><td>$orderno</td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>";
}

答案 2 :(得分:0)

如果我理解正确

foreach ($retrievependingorders as $pendingorders){
        $orderno = $pendingorders['order_no'];
        $lastupdate = $pendingorders['order_update_date'];
        $orderstatus = $pendingorders['order_status'];
        $totalprice = $pendingorders['total_price'];
        $sum_totalprice +=$totalprice

        echo "<tr><td><a href=\"/enter-online-icas/my-orders/order-details/?orderno=".$orderno."\">$orderno</a></td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>";
}
echo "<tr><td></td><td></td><td></td><td>".$sum_totalprice."</td></tr>";

答案 3 :(得分:0)

您需要创建一个变量来保存运行计数。

PHP

$grand_total_price = 0; //Initialize your variables

foreach ($retrievependingorders as $pendingorders){
    $orderno = $pendingorders['order_no'];
    $lastupdate = $pendingorders['order_update_date'];
    $orderstatus = $pendingorders['order_status'];
    $totalprice = $pendingorders['total_price'];
    $grand_total_price += $totalprice;

    echo "<tr><td>$orderno</td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>";

}

回应“我的总计:”。 $ grand_total_price。 “”;