1 回答
TA贡献1863条经验 获得超2个赞
您可以使用array_count_values()来了解提供了每个答案的数量:
$counts = array_count_values($_POST['q']);
// e.g.: ["A" => 2, "B" => 1, "F" => 1]
然后,您可以通过查看结果来回显每个答案的数字:
echo "A was answered " . ($counts["A"] ?? 0) . " time(s)<br>";
echo "B was answered " . ($counts["B"] ?? 0) . " time(s)<br>";
echo "C was answered " . ($counts["C"] ?? 0) . " time(s)<br>";
echo "D was answered " . ($counts["D"] ?? 0) . " time(s)<br>";
echo "E was answered " . ($counts["E"] ?? 0) . " time(s)<br>";
echo "F was answered " . ($counts["F"] ?? 0) . " time(s)<br>";
如果您只想列出实际给出的答案,则可以遍历$counts数组:
foreach ($counts as $answer => $count) {
echo $answer . " was answered " . $count . " time(s)<br>";
}
- 1 回答
- 0 关注
- 138 浏览
添加回答
举报