在子组中分组结果 [英] Group results in subgroups
本文介绍了在子组中分组结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试使用接受的答案中的方法得到以下数据的小组总数:如何将结果分组到php 中,但无济于事。
我想要这个:
+ - ------------- + ----------------- +
|类型|价格|
+ --------------- + ----------------- +
|音乐| 19.99 |
|音乐| 3.99 |
|音乐| 21.55 |
|玩具| 89.95 |
|玩具| 3.99 |
+ --------------- + ----------------- +
$ b显示为:
音乐| 45.53
玩具| 93.94
在小组和小计中。
解决方案试试这个...
//获取sum
$ group = array();
foreach($ rows为$ r){
$ group [$ r-> type] = $ group [$ r->类型] + $ r->价格;
}
//显示
foreach($ group as $ type => $ price){
echo类型:。$ type。Price: $价格;
}
I have been trying to get the subgroup total of the data below using the method in the accepted answer here: how to group result in subgroups in php, but to no avail. Somebody please help.
I want this:
+---------------+-----------------+
| Type | Price |
+---------------+-----------------+
| Music | 19.99 |
| Music | 3.99 |
| Music | 21.55 |
| Toy | 89.95 |
| Toy | 3.99 |
+---------------+-----------------+
displayed as this:
Music | 45.53
Toy | 93.94
in group and subtotal.
解决方案
Try this...
// Get the sum
$group = array();
foreach($rows as $r){
$group[$r->type] = $group[$r->Type] + $r->Price;
}
// Display
foreach($group as $type=>$price){
echo "Type:".$type." Price: ".$price;
}
这篇关于在子组中分组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文