在php中添加一个

I have a problem with my addition : So I have this code :

                $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;

So I want to save in total_montant the last value, but not work and I don't understand why...Can you help me please ? Thx in advance

You should also do:

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

otherwise you do not add anything (since $total=0;)

So you get:

           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;

            }

update your code to be:

$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 ;

because in your code you $history['montant'] to $total but you didn't assign the result to $total

Try this:

            $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;