用 php 做一个插件


Make an adittion in php

我的添加有问题:所以我有这个代码:

                $total = 0;
                foreach(getHistory($this->id) as $history){
                    $aHistoryFilter['date']                 = $history['date'];
                    $aHistoryFilter['ls']                   = $history['ls']);
                    $aHistoryFilter['montant']              = $history['montant'];
                    $aHistoryFilter['total_montant']        = $total+$history['montant'];
                    $aHistory[] = $aHistoryFilter;
                }
                return $aHistory;

所以我想保存total_montant最后一个值,但不起作用,我不明白为什么......你能帮我吗?提前感谢

您还应该执行以下操作:

$total  = $total + $history['montant'];

否则您不会添加任何内容(因为$total=0;

所以你得到:

           foreach(getHistory($this->id) as $history){
                $aHistoryFilter['date']                 = $history['date'];
                $aHistoryFilter['ls']                   = $history['ls']);
                $aHistoryFilter['montant']              = $history['montant'];
                $aHistoryFilter['total_montant']        = $total+$history['montant'];
                $total  = $total + $history['montant'];
                $aHistory[] = $aHistoryFilter;
            }

将代码更新为:

$total = 0;
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date']                 = $history['date'];
$aHistoryFilter['ls']                   = $history['ls']);
$aHistoryFilter['montant']              = $history['montant'];
$total       = $total+$history['montant'];
$aHistory[] = $aHistoryFilter;
}
$aHistoryFilter['total_montant'] = $total ;

因为在代码中,您$history['montant'] $total但未将结果分配给$total

试试这个:

            $total = 0;
            foreach(getHistory($this->id) as $history){
                $aHistoryFilter['date']                 = $history['date'];
                $aHistoryFilter['ls']                   = $history['ls']);
                $aHistoryFilter['montant']              = $history['montant'];
                // this adds the $history['montant'] to the $total
                $total                                 += $history['montant'];
                // this adds the $total to your variable
                $aHistoryFilter['total_montant']        = $total;
                $aHistory[] = $aHistoryFilter;
            }
            return $aHistory;