Home > Software engineering >  How to obtain answers when there are more than one correct answer using an array using php
How to obtain answers when there are more than one correct answer using an array using php

Time:10-01

I have an issue with checking the correct answers from two arrays. The first are the correct answers, where it can have either one up to four possible correct answers.

{
"17":"nukequiz",
"18":"",
"13":"hard dRiVe",
"20":"false",
"15":"nukequiz",
"16":"National Speed Limit",
"14":"nukequiz",
"19":["Hue Edwards","Mark Spencer","Mark Smith"]
}

How can I check that each of the users answers for question 19 matches the correct answer?

[correct_answer] => Kate Lewington,Mark Smith

I have tried using array diff, but that just tell me if there are more answers from the user than there are correct answers! This is useful, but does not check each user answer against each correct answer.

The code I have is not exactly great, but does work for either short, multiple choice single answers and true or false answers.

    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  ;
        }

// 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);
$cntanswers = count($cAnswer);

/////////////////SOLUTION

 $i = $z = 0;
    foreach($uAnswer as $val){
            if(in_array($val, $cAnswer)){
           echo '<tr style="background-color:#090">
         <td><div ></div>
         </td><td>'.$quesno.'</td>
                    <td>Correct Answer:  '.$row['correct_answer'].'</td>
                    <td>User Answer:  '.$val.'</td>';

// For each user's answer, it creates a line in the table

$i  ;
$points = $row['points'];       
    $add = $points/$cntanswers;
                echo '<td>Points:  '.$add.'</td></tr>';
                $score = $score   $add;
            }
            elseif (!in_array($val, $cAnswer)){
                 echo '<tr style="background-color:#900">
       <td><div ></div>
       </td><td>'.$quesno.'</td>
       <td>Correct Answer:  '.$row['correct_answer'].'</td>';
                $z  ;
     echo '<td>Wrong Answer:  '.$val.' ['.$z.']</td></tr>'; 
            
            }
        }

///////////////////////////===End Solution

} 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  ;
    }
}

I need any ideas as to how to separate the user answers and check them against each correct answer.

Array diff is useful but only tells me what is not in the correct answer array.

Any help would be gratefully received if someone can at least point me in the right direction.

CodePudding user response:

The solution is marked with '// ////////////////SOLUTION'. I used some test data to run the code.

$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>';

I hope it helps.

  • Related