Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php foreach中计算列的和_Php_Mysql - Fatal编程技术网

Php foreach中计算列的和

Php foreach中计算列的和,php,mysql,Php,Mysql,我有一张mysql表 服务费 id service_name service_charges 我使用mysql查询,比如 select service_name service, service_charges,count(service_name)as unit from service charges group_by service 在我使用的php文件中 foreach(services as service){ echo $service['service']; echo $s

我有一张mysql表

服务费

id service_name service_charges 
我使用mysql查询,比如

select service_name service, service_charges,count(service_name)as unit 
from service charges group_by service
在我使用的php文件中

foreach(services as service){
echo $service['service'];
echo $service['service_charges'];
echo $service['unit'];
$service_charges_total = (($service['service_charges'])*($service['unit']));
echo $sevice_charges_total;
}
我得到的结果如下:

现在,我如何获得$service_charges_total(金额列)的总额,即我想要的总额为4200+1200+350=5750


谢谢。

您可以在php中使用以下函数进行操作

$total=0;
foreach(services as service)
{
   echo $service['service'];
   echo $service['service_charges'];
   echo $service['unit'];
   $service_charges_total = (($service['service_charges'])*($service['unit']));
   echo $sevice_charges_total;
   $total+=$sevice_charges_total;
}
echo $total;

我希望这对你有用

将金额值存储在数组中,然后使用数组_sum()


$serviceTotalAmount+=$service\u charges\u total;
select service_name as service, 
       service_charges,
       count(service_name) as unit,
       (select sum(service_charges) from service) as total_amount
from service as charges 
group_by service, 
         service_charges
SELECT sum(Amount) from service_charges;
$service_charges_total = (($service['service_charges'])*($service['unit']));
$array[]=$service_charges_total;
$total_sum=array_sum($array);
echo $total_sum;