我有提交的调查,我想统计结果。每个调查都保存在一个多维数组中,如下所示:
Array ( [name] => Clark Kent [rating] => 5 )
这些来自循环,因为它们是单独的数据库条目。
所以我首先创建一个包含所有这些组合的新数组:
$mods = array();
$index = -1;
foreach($fields as $field) {
$index++;
$mods[$index]['name'] = $field['name'];
$mods[$index]['rating'] = $field['rating'];
}
然后我将这些分组,以便将同名的所有评级放在一起,以便我可以稍后对它们进行汇总。
$groups = array();
foreach ($mods as $value) {
$groups[$value['name']][] = $value;
}
这会产生以下结果:
Array (
[Clark Kent] => Array (
[0] => Array (
[name] => Clark Kent
[rating] => 5
)
[1] => Array (
[name] => Clark Kent
[rating] => 5
)
)
[Peter Parker] => Array (
[0] => Array (
[name] => Peter Parker
[rating] => 5
)
[1] => Array (
[name] => Peter Parker
[rating] => 5
)
)
[Bruce Wayne] => Array (
[0] => Array (
[name] => Bruce Wayne
[rating] => 5
)
[1] => Array (
[name] => Bruce Wayne
[rating] => 5
)
)
[Bruce Banner] => Array (
[0] => Array (
[name] => Bruce Banner
[rating] => 5
)
[1] => Array (
[name] => Bruce Banner
[rating] => 5
)
)
)
我想要完成的事情是这样的:
<table>
<tr>
<td>Clark Kent</td>
<td>{average of all ratings}</td>
</tr>
</table>
我大部分时间都在那里,但我被卡住了!我不确定如何获得name没有任何类型的索引或键的分组,以便我可以将该值用于我的表。然后我需要对每个分组的值求和。
拉莫斯之舞
红糖糍粑
慕桂英4014372