Note that there are some explanatory texts on larger screens.

plurals
  1. POCombine Multiple Foreach Results To One Variable String
    text
    copied!<p>I have multiple foreach loops below, and I'm trying to join one set of data inside one foreach, with another set of data in the other foreach, so I can create one main variable to post anywhere on my page with these values. I thought the way I had it setup below would work, but it's not. Any suggestions?</p> <p>The variables I have setup right now, with the data I need to collect is <code>$variable</code></p> <pre><code>foreach($result as $row) { $sections[$row['section_id']][$row['question_id']] = $row; } foreach ($sections as $key =&gt; $section) { $question_keys = array_keys($section); foreach ($question_keys as $question_key) { if ($section[$question_key]["selection"] == 2) { } else { $variable .= '&lt;div id="section-title"&gt;'.$section_details[$key].'&lt;/div&gt;'; foreach ($section as $key =&gt; $questions) { $variable .= '&lt;div id="section-desc"&gt; &lt;div&gt;'.$question_details[$key]["title"].'&lt;/div&gt; &lt;div&gt;'.$question_details[$key]["desc"].'&lt;/div&gt; &lt;div&gt;'.$selection_details[$questions["selection"]].'&lt;/div&gt;&lt;/div&gt;'; } } } } </code></pre> <p>The first <code>$variable</code> assigned is a section. Sections nest questions. There can be multiple sections nesting questions that belong to it. The second <code>$variable</code> assigned is questions nested underneath the section. Questions can be multiple. <br /><br /><br /> <strong>Array output at</strong> <code>$section</code> <strong>level</strong></p> <pre><code>Array ( [2] =&gt; Array ( [assessment_selection_id] =&gt; 97 [assessment_id] =&gt; 32 [section_id] =&gt; 2 [question_id] =&gt; 2 [selection] =&gt; 1 [timestamp] =&gt; 1368183565 ) [1] =&gt; Array ( [assessment_selection_id] =&gt; 98 [assessment_id] =&gt; 32 [section_id] =&gt; 2 [question_id] =&gt; 1 [selection] =&gt; 3 [timestamp] =&gt; 1368183580 ) ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload