Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组答案的PHP问答计算_Php_Html_Arrays - Fatal编程技术网

数组答案的PHP问答计算

数组答案的PHP问答计算,php,html,arrays,Php,Html,Arrays,我有一个使用html单选按钮设计的测验,计算由一些PHP处理 请参见下面的基本html布局(为了便于阅读,删除了一些元素) 如何仅为完全正确的答案分配一分?如果您只想计算完全正确的答案,则可以将预期答案的数量与匹配的数量进行比较,因此将第一个If部分更改为 if ( is_array($solution) ){ $correct = array_intersect($solution, $userAnswer); $total += (count($solution) == co

我有一个使用html单选按钮设计的测验,计算由一些PHP处理

请参见下面的基本html布局(为了便于阅读,删除了一些元素)


如何仅为完全正确的答案分配一分?

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

if ( is_array($solution) ){
    $correct = array_intersect($solution, $userAnswer);
    $total += (count($solution) == count($correct));
}

$marksPerAnswer
是任何答案的小数部分,因此对于两个答案,
$marksPerAnswer
将为0.5。如果您想给每个答案打多个分数,那么您可能需要扩展
$solutions
数组以包含此字段的另一个字段(以及处理此字段的额外代码)。将
*5
放在括号外-
计数($correct))*5
当你说仍然不起作用时,结果应该是0分或5分(只有在所有答案都正确的情况下才是5分),这是你想要的吗?好的-循环后将
$total
乘以5做同样的事情吗?我已经创建了另一个类似的问题,如果你有时间建议答案,那将是greta,谢谢!
$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);
    }
}
if ( is_array($solution) ){
    $correct = array_intersect($solution, $userAnswer);
    $total += (count($solution) == count($correct));
}