2

I have a problem, were I need to loop through an array inside a "foreach-loop". The PHP code below is per now looping through the JSON and looking for ["Question"], and looping these correctly.

But these questions have different amounts of ["Answers"]. Also this JSON is made dynamically so I don't know from before how many questions/answers there'll be.

So what I need is the ["Answers"] should be looped withing the <li> inside each question.

Would be really greatful for some expertise here!

JSON

     ["Questions"]=>
      array(2) {
        [0]=>
        object(stdClass)#13 (2) {
          ["Question"]=>
          string(30) "My first questions is this...."
          ["Answers"]=>
          array(2) {
            [0]=>
            object(stdClass)#14 (2) {
              ["Answers"]=>
              string(18) "I like this answer"
              ["Correct"]=>
              bool(true)
            }
          }
        }
        [1]=>
        object(stdClass)#16 (2) {
          ["Question"]=>
          string(22) "Another funny question"
          ["Answers"]=>
          array(2) {
            [0]=>
            object(stdClass)#17 (2) {
              ["Answers"]=>
              string(9) "Or is it?"
              ["Correct"]=>
              bool(false)
            }
            [1]=>
            object(stdClass)#18 (2) {
              ["Answers"]=>
              string(10) "Yes it is!"
              ["Correct"]=>
              bool(true)
            }
          }
        }
      }

PHP

$questions = array();

$i = 0;
foreach($question as $key => $items) {
$i++;

if ($i>1) {$hide=' hide';}
$questions[] = "
            <div class='question-holder" . $hide . "' id='q" . $i . "'>   
                <h2>" . $items->Question . "</h2>
                    <ul class='answers' id='quiz-answers" . $i . "'>
                        <li>
                            <input tabindex='1' type='radio' name='txtAnswer" . $i . "' id='txtAlt1' value='sdsds'>
                            <label for='txtAlt1'><span>" . $items->Answers[0]->Answers . "</span></label>
                        </li>
                    </ul>
            </div>";
} 
3
  • if you understand how to use a foreach loop with an array, you can json_decode your array and loop through it. Commented Aug 16, 2013 at 22:56
  • You got a Parse error: syntax error, unexpected '{' on line 5 Commented Aug 16, 2013 at 23:09
  • 1
    Thanks for the observation @raam86 ! Accidentaly copied an extra one, but I remove it now Commented Aug 16, 2013 at 23:12

1 Answer 1

2

Use a nested loop for the answers.

$tab = 0;
foreach ($question as $items) {
    $i++;
    $j = 0;
    if ($i > 1) { $hide = ' hide'; }
    $this_q = "<div class='question-holder" . $hide . "' id='q" . $i . "'>   
            <h2>" . $items->Question . "</h2>
                <ul class='answers' id='quiz-answers" . $i . "'>
    ";
    foreach ($items->Answers as $ans) {
        $tab++;
        $j++;
        $qa = "$i-$j";
        $this_q .= "<li>
                        <input tabindex='" . $tab . "' type='radio' name='txtAnswer" . $qa . "' id='txtAlt" . $qa . "' value='sdsds'>
                        <label for='txtAlt" . $qa . "'><span>" . $ans->Answers . "</span></label>
                    </li>
        ";
    }
    $this_q .= "</ul>
        </div>";
    $questions[] = $this_q;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for a great explaination, just made a small adjustment at it works perfectly! Thanks again!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.