亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數組答案的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 關注
  • 127 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號