-1

PHP または JavaScript、できれば PHP に 2 つの配列がある場合、正しい答えの割合を取得するにはどうすればよいですか?

したがって、これらの 2 つの配列があり、クイズの結果を正解と比較して、パーセンテージ スコアを取得したいと考えています。

$quiz_results = array( 'q1' => 'no',
                    'q2' => 'yes',
                    'q3' => 'no',
)

$answers = array( 1 => 'yes',
                  2 => 'no',
                  3 => 'yes'
)
4

2 に答える 2

3

答えを確認し、質問と比較します。それらが同じである場合、正答カウントがインクリメントされます。

$quiz_results = array( 'q1' => 'yes',
                    'q2' => 'yes',
                    'q3' => 'no',
);
$answers = array( 1 => 'yes',
                  2 => 'no',
                  3 => 'yes'
);

$totalquestions = count($answers);
$correct = 0;
foreach($answers as $key => $answer){
    //q + the key should do it. Its easier if they are the same obviously
    if($answer == $quiz_results['q'.$key]){
        // correct 
        $correct++;
    }
}

echo 100 / $totalquestions * $correct; //returns 33.333333%
于 2015-09-23T13:58:04.370 に答える