Kim
Kim

Reputation: 1156

How do I "loop" through JSON array inside foreach loop?

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

Upvotes: 2

Views: 1062

Answers (1)

Barmar
Barmar

Reputation: 780798

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

Upvotes: 2

Related Questions