1

total n00b to PHP but very interested. Struggling right now with using a deep multidimensional array as part of a single-form php quiz, where the multidimensional array must contain both the options and the answers for every question on the quiz.

Here is the first part of the array, that I am specifically looking at:

$quizquestions = array  ("question1" => array ("type" => "checkbox",
                                                    "text" => "question text goes here",
                                                    "options" => array ([1] => "option 1 goes here",
                                                                        [2] => "option 2 goes here",
                                                                        [3] => "option 3 goes here",
                                                                        [4] => "options 4 goes here",
                                                                        [5] => "option 5 goes here",
                                                                        [6] => "option 6 goes here"

                                                                        ), //end of options array
                                                    "answers" => array  ([1] => " right answer one here",
                                                                         [3] => "right answer 2",
                                                                         [6] => "right answer 3 here"
                                                                        ), //end of  answers array
                                                    "feedback" => "text that is returned after user answers"                                                    
                                                ), //end of question 1 array... question 2 begins, etc...

In the above example, question 1 is a checkbox, with 3 possible answers that are considered correct (1, 3, and 6). I've been trying nested foreach loops to get to the options of my questions, but can't seem to get the syntax right, or figure out what function i need to use to evaluate the the against the answer array - can you use in_array on something you've fed through several foreach statements; on multidimensional arrays?

I've been hoping to print out the responses in a list, with either checks or X's next to the responses the user submits, depending on whether or not they are in the answers array.

Here's what I've been trying so far, but it is not returning anything. I've tried a million variations but may just be on the wrong track...

foreach ($quizquestions as $question => $description) {
            foreach ($description as $option => $response) {
            if (in_array($option, $response['question1']['answers'])) {
    print "<li class='correct'>$options</li><br />\n";
    $score++;
    } else {
    print "<li class='incorrect'>$options</li><br />\n";
    } //end of if(in_array() ) 
    } //end of description foreach
} //end of quizquestion foreach

Any thoughts? Help a newbie out please!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.