我在检查两个数组的正确答案时遇到问题。第一个是正确答案,可以有一个到四个可能的正确答案。
{
"17":"nukequiz",
"18":"",
"13":"hard dRiVe",
"20":"false",
"15":"nukequiz",
"16":"National Speed Limit",
"14":"nukequiz",
"19":["Hue Edwards","Mark Spencer","Mark Smith"]
}
我如何检查每个用户对问题19的回答是否与正确答案匹配?
[correct_answer]=>Kate Lewington,Mark Smith
我试过使用数组diff,但这只是告诉我用户的答案是否比正确答案多!这很有用,但不会将每个用户的答案与每个正确答案进行核对。
我的代码并不是很好,但确实适用于简短的多选单选题和正确或错误的答案。
foreach($result as $row) {
if(!is_array($data[$row['quesid']]) &&
!is_array($row['correct_answer']))
{
$row['correct_answer'] = strtolower($row['correct_answer']);
$data[$row['quesid']] = strtolower($data[$row['quesid']]);
}
if($row['correct_answer'] == $data[$row['quesid']]){
echo '<tr style="background-color:#060">
<td colspan="2">Correct Answer: '.$row['correct_answer'].'</td>
<td colspan="2">User Answer: '.$data[$row['quesid']].'</td>
</tr>';
$points = $row['points'];
$score = $score + $points;
$right_answer++;
}
//使用nukequiz检查文本框中的空字符串或无线电组中的隐藏值以显示其空值。不使用隐藏字段总是导致"隐藏";否";由于Jquery而显示。
elseif($data[$row['quesid']] == 'nukequiz' || $data[$row['quesid']] == "" )
{
echo '<tr style="background-color:#999">
<td colspan="2">Correct Answer: '.$row['correct_answer'].'</td>
<td colspan="2">User Answer: '.$data[$row['quesid']].'</td>
</tr>';
$unanswered++;
} elseif(!is_string($data[$row['quesid']]) &&
is_array($data[$row['quesid']]))
{
$cAnswer = explode (",", $row['correct_answer']);
$uAnswer = $data[$row['quesid']];
$cAnswer = array_map( 'strtolower', $cAnswer );
$uAnswer = array_map( 'strtolower', $uAnswer );
array_multisort($cAnswer);
array_multisort($uAnswer);
$cntanswers = count($cAnswer);
} else {
echo '<tr style="background-color:#930">
<td colspan="2">Correct Answer: '.$row['correct_answer'].'</td>
<td colspan="2">User Answer: '.$data[$row['quesid']].'</td></tr>';
$wrong_answer++;
}
}
我需要任何关于如何将用户答案分开并将其与每个正确答案进行核对的想法。
数组diff很有用,但只有告诉我正确答案数组中没有的内容。
如果有人能为我指明正确的方向,我们将不胜感激。
解决方案标有"/////////\///////解决方案"。我使用了一些测试数据来运行代码。
$result = array(
array(
'quesid' => 13,
'correct_answer' => 'hard dRiVe',
'points' => 10
),
array(
'quesid' => 16,
'correct_answer' => 'National Speed Limit',
'points' => 10
),
array(
'quesid' => 19,
'correct_answer' => 'Hue Edwards,Mark Spencer,Mark Smith',
'points' => 10
),
);
$data = array(
13 => 'Wrong Answer',
16 => 'National Speed Limit',
19 => array('Hue Edwards')
);
echo '<table>';
foreach($result as $row) {
echo '==='.$row['quesid'].'===<hr>';
echo '==='.$row['correct_answer'].'===<hr>';
echo '==='.$row['points'].'===<hr>';
echo '==='.$data[$row['quesid']].'===<hr>';
// echo $k['boards']['price'];
//$Canswer = array_map( 'strtolower', $row['correct_answer'] );
if(!is_array($data[$row['quesid']]) &&
!is_array($row['correct_answer']))
{
$row['correct_answer'] = strtolower($row['correct_answer']);
$data[$row['quesid']] = strtolower($data[$row['quesid']]);
}
if($row['correct_answer'] == $data[$row['quesid']]){
echo '<tr style="background-color:#060">
<td colspan="2">Correct Answer: '.$row['correct_answer'].'</td>
<td colspan="2">User Answer: '.$data[$row['quesid']].'</td>
</tr>';
$points = $row['points'];
$score = $score + $points;
$right_answer++;
}
// Checks for either empty string from text box or hidden value in radio group using nukequiz to show its empty. Using no hidden field always results in "no" showing up due to to Jquery.
elseif($data[$row['quesid']] == 'nukequiz' || $data[$row['quesid']] == "" )
{
echo '<tr style="background-color:#999">
<td colspan="2">Correct Answer: '.$row['correct_answer'].'</td>
<td colspan="2">User Answer: '.$data[$row['quesid']].'</td>
</tr>';
$unanswered++;
} elseif(!is_string($data[$row['quesid']]) &&
is_array($data[$row['quesid']]))
{
$cAnswer = explode (",", $row['correct_answer']);
$uAnswer = $data[$row['quesid']];
$cAnswer = array_map( 'strtolower', $cAnswer );
$uAnswer = array_map( 'strtolower', $uAnswer );
array_multisort($cAnswer);
array_multisort($uAnswer);
// ////////////////SOLUTION
foreach($uAnswer as $answer){
if(in_array($answer, $cAnswer)){
echo '<tr style="background-color:#999">
<td colspan="2">Correct Answer: '.$row['correct_answer'].'</td>
<td colspan="2">User Answer: '.$answer.'</td>
</tr>'; // For each user's answer, it creates a line in the table
}
}
///////////////////////////
} else {
echo '<tr style="background-color:#930">
<td colspan="2">Correct Answer: '.$row['correct_answer'].'</td>
<td colspan="2">User Answer: '.$data[$row['quesid']].'</td>
</tr>';
$wrong_answer++;
}
}
echo '<table>';
我希望它能有所帮助。