为了账号安全,请及时绑定邮箱和手机立即绑定

数组答案的PHP测验计算

数组答案的PHP测验计算

PHP
慕森卡 2023-04-28 14:37:31
我有一个使用 html 单选按钮设计的测验,计算由一些 PHP 处理。请参阅下面的基本 html 布局(为了便于阅读,删除了一些元素);// q1 answer is value 2<input type="radio" name="form[1-1]" value="1"><input type="radio" name="form[1-1]" value="2">// q2 answer is value 1<input type="radio" name="form[1-2]" value="1"><input type="radio" name="form[1-2]" value="2">// q1 answer is value 2<input type="radio" name="form[1-3]" value="1"><input type="radio" name="form[1-3]" value="2">// q4 answer is value 1 AND 2 AND 4<input type="checkbox" name="form[1-4][]" value="1"><input type="checkbox" name="form[1-4][]" value="2"><input type="checkbox" name="form[1-4][]" value="3"><input type="checkbox" name="form[1-4][]" value="4">我目前使用的 PHP 代码(多亏了PHP Quiz Radio 和 Checkbox Calculation),但是它在具有多个答案的问题上拆分了要点。$solutions = ['1-1' => 2, '1-2' => 1, '1-3' => 2, '1-4' => [1,2,4]];foreach ( $solutions as $question => $solution ) {    $userAnswer = $_POST['form'][$question] ?? null;    if ( is_array($solution) ){        $marksPerAnswer = 1/count($solution);        $correct = array_intersect($solution, $userAnswer);        $total += $marksPerAnswer * count($correct);    }    else    {        $total += ($userAnswer == $solution);    }}如何只为完全正确的答案分配一分?
查看完整描述

1 回答

?
四季花海

TA贡献1811条经验 获得超5个赞

如果您只想计算完全正确的答案,那么您可以将预期答案的数量与匹配的数量进行比较,因此将第一个 if 部分更改为...


if ( is_array($solution) ){

    $correct = array_intersect($solution, $userAnswer);

    $total += (count($solution) == count($correct));

}


查看完整回答
反对 回复 2023-04-28
  • 1 回答
  • 0 关注
  • 70 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信