在php中创建一个ADITION

在php中创建一个ADITION,php,php-5.3,Php,Php 5.3,我的添加有问题: 所以我有这个代码: $total = 0; foreach(getHistory($this->id) as $history){ $aHistoryFilter['date'] = $history['date']; $aHistoryFilter['ls']

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

                $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\u montant
中,但不起作用,我不明白为什么…你能帮我吗?Thx提前

您还应执行以下操作:

$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  = $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'];
$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;
试试这个:

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

尝试放置$ahistore[]=$ahistorefilter;在$ahistorefilter['date']之前,您从不更新
$total
,因此实际上您正在执行
total\u montant=0+montant
,这归结为
total\u montant=montant
尝试将$ahistore[]=ahistorefilter;在$aHistoryFilter['date']之前,您从未更新过
$total
,因此实际上您正在执行
total\u montant=0+montant
,这归结为
total\u montant=montant
您迟到了几秒钟;P@BasvanStein没问题。我会投你一票让我来报答你的好意,毕竟所有的伟人都这么想;P@BasvanStein没问题。我会投你一票让我来回报你,毕竟所有的伟人都有相同的想法。