PHP Foreach Array -- Displaying One Result at a Time

时间:2016-04-04 16:45:36

标签: php arrays json foreach

I have an array of questions that can by dynamically larger or smaller like this:

[questions] => Array
                    (
                        [0] => stdClass Object
                            (
                                [prompt] => With which of the following states are you or have you been associated?
                                [type] => state
                                [answers] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [text] => PENNSYLVANIA
                                                [correct] => 
                                            )

                                        [1] => stdClass Object
                                            (
                                                [text] => KANSAS
                                                [correct] => 
                                            )

                                        [2] => stdClass Object
                                            (
                                                [text] => WISCONSIN
                                                [correct] => 1
                                            )

                                        [3] => stdClass Object
                                            (
                                                [text] => UTAH
                                                [correct] => 
                                            )

                                        [4] => stdClass Object
                                            (
                                                [text] => None of the above
                                                [correct] => 
                                            )

                                    )

                            )

                        [1] => stdClass Object
                            (
                                [prompt] => With which of the following companies have you ever been employed?
                                [type] => company
                                [answers] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [text] => Alternator Engineers and Fabricators
                                                [correct] => 
                                            )

                                        [1] => stdClass Object
                                            (
                                                [text] => Personal and Corporate Design
                                                [correct] => 
                                            )

                                        [2] => stdClass Object
                                            (
                                                [text] => PRECISE PHOTOGRAPHY
                                                [correct] => 
                                            )

                                        [3] => stdClass Object
                                            (
                                                [text] => EVERYTHING FLORAL
                                                [correct] => 
                                            )

                                        [4] => stdClass Object
                                            (
                                                [text] => None of the above
                                                [correct] => 1
                                            )

                                    )

                            )

                        [2] => stdClass Object
                            (
                                [prompt] => Which of the following addresses are in Green Bay?
                                [type] => address
                                [answers] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [text] => 87 EAST BROADWAY BLVD
                                                [correct] => 1
                                            )

                                        [1] => stdClass Object
                                            (
                                                [text] => 8 COLUMBIA STREET
                                                [correct] => 
                                            )

                                        [2] => stdClass Object
                                            (
                                                [text] => 1 WEST IRONWOOD DRIVE
                                                [correct] => 
                                            )

                                        [3] => stdClass Object
                                            (
                                                [text] => 9552 BRENTWOOD DRIVE
                                                [correct] => 
                                            )

                                        [4] => stdClass Object
                                            (
                                                [text] => None of the above
                                                [correct] => 
                                            )

                                    )

                            )

What I am attempting to do is write a foreach loop to display the questions but i need them "one at a time" with a next button and then submit the answers to my scoring process.

Here is my foreach

foreach (array_slice($json_result->output->questions->questions, 0, 1) as $response){
    prompts = $response->prompt; 
    $type = $response->type;
    echo"<div><h2>$prompts $type</h2>";

    foreach ($response->answers as $answers){
        $answer = $answers->text; 
        $correct = $answers->correct; 

        echo "<ul>
          <li>
            <input id='cb10' name='answer[]' value='$correct' type='checkbox'>
            <label for='cb10'>$answer</label>
          </li>
        </ul>
        </div>";
    }    
}

What i want to do is put each set of questions and answers in a div dynamically and then i will write css to show the next div on button click. Can someone help me get them in the div properly?

0 个答案:

没有答案