3 回答
TA贡献1820条经验 获得超10个赞
我会在从数据库读取数据的循环中做所有必要的数学运算。像这样的东西:
$ratings = array();
while ($row = $result->fetch_assoc()) {
$name = $row['name'];
if (!isset($ratings[$name])) {
$ratings[$name] = array('count' => 1, 'sum' => $row['rating']);
}
else {
$ratings[$name]['count']++;
$ratings[$name]['sum'] += $row['rating'];
}
}
然后你可以像这样输出你的表格:
echo "<table>";
foreach ($ratings as $name => $r) {
echo "<tr><td>$name</td><td>" . round($r['sum'] / $r['count'], 1) . "</td></tr>";
}
echo "</table>";
TA贡献1815条经验 获得超6个赞
要获得平均值,您可以执行以下操作:
foreach ($groups as $name => $group) {
$average = array_sum(array_column($group, 'rating')) / count($group);
echo $name;
}
TA贡献1871条经验 获得超13个赞
您可以首先在用于处理这些日期的结构中简化问题
foreach($fields as $field) {
$mods[$field['name']][] = $field['rating'];
}
然后只用关键参数 foreach
foreach($mods as $name => $mod) {
echo $name;
echo array_sum($mod) / count($mod);
}
- 3 回答
- 0 关注
- 203 浏览
添加回答
举报