title: Using foreach to echo data from JSON in PHP
body: <p>Basically what I have are two JSON files. One JSON file contains a set of questions and the second JSON file contains the user answers. What I want to do is echo the questions from the one json file and echo out the answers from the other inside a div.</p>
  
  <p>I tried putting a foreach within a foreach. This does work to a certain extent but it echo's the questions multiple times for each answer.</p>
  
  <pre><code>&lt;?php
  $questions = "../induction.json";
  $contents = file_get_contents($questions,0,null,null);
  $qArray = json_decode($contents, JSON_PRETTY_PRINT);
  
  $answers = $user."_induction.json";
  $contentsA = file_get_contents($answers,0,null,null);
  $qArrayA = json_decode($contentsA, JSON_PRETTY_PRINT);
  
  ?&gt;
  
  &lt;div role="tabpanel" class="tab-pane fade" id="messages_with_icon_title"&gt;
  &lt;b&gt;Quiz&lt;/b&gt;
   &lt;?php
      $num = 0;
      foreach($qArray as $key =&gt; $value){
         foreach($qArrayA as $key2 =&gt; $value2){
   ?&gt;
   &lt;form method="POST"&gt;
   &lt;div class="row clearfix"&gt;
     &lt;div class="col-lg-4 col-md-4 col-sm-6 col-xs-12"&gt;
       &lt;div class="card"&gt;
          &lt;div class="header"&gt;
              &lt;h2&gt;&lt;?php echo $value['question']; ?&gt;&lt;/h2&gt;
           &lt;/div&gt;
           &lt;div class="body"&gt;
           &lt;?php
               echo $value2;
           ?&gt;
        &lt;/div&gt;
     &lt;/div&gt;
   &lt;/div&gt;
  &lt;/div&gt; 
  &lt;?php   } 
  } ?&gt;
  &lt;input type="submit" class="btn btn-success waves-effect" name="submit" value="Submit" /&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This is the result: <a href="https://imgur.com/a/sQEQGBz" rel="nofollow noreferrer">https://imgur.com/a/sQEQGBz</a></p>