Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop through array in PHP
    text
    copied!<p>I have an array that I've created that is made up of sections and questions. How can I loop through the sections and display the nested questions of each section.</p> <p><strong>Here is how I'm creating the array</strong></p> <pre><code>$db = db_open(); $query = "SELECT * FROM assessment_selections WHERE assessment_id = '".$annual_assessment["id"]."' AND selection = '1' ORDER BY timestamp ASC"; $result = db_query($db, $query); $result = db_fetch_all($result); if (!is_array) $result = array(); foreach($result as $row) { $section[$row['section_id']][$row['question_id']] = $row; } </code></pre> <p><strong>Here is the array</strong></p> <pre><code>Array ( [1] =&gt; Array // Section 1 ( [1] =&gt; Array // Question 1 ( [assessment_selection_id] =&gt; 70 [assessment_id] =&gt; 32 [section_id] =&gt; 1 [question_id] =&gt; 1 [selection] =&gt; 1 [timestamp] =&gt; 1368172762 ) ) [2] =&gt; Array // Section 2 ( [3] =&gt; Array // Question 3 ( [assessment_selection_id] =&gt; 68 [assessment_id] =&gt; 32 [section_id] =&gt; 2 [question_id] =&gt; 3 [selection] =&gt; 1 [timestamp] =&gt; 1368166250 ) ) [3] =&gt; Array // Section 3 ( [4] =&gt; Array // Question 4 ( [assessment_selection_id] =&gt; 69 [assessment_id] =&gt; 32 [section_id] =&gt; 3 [question_id] =&gt; 4 [selection] =&gt; 1 [timestamp] =&gt; 1368172690 ) ) [4] =&gt; Array // Section 4 ( [5] =&gt; Array // Question 5 ( [assessment_selection_id] =&gt; 71 [assessment_id] =&gt; 32 [section_id] =&gt; 4 [question_id] =&gt; 5 [selection] =&gt; 1 [timestamp] =&gt; 1368174153 ) ) ) </code></pre> <p><br /><br /> <strong>Expected results</strong> (How I would like to be able to echo them out in PHP)</p> <p><strong>Section 1</strong></p> <ul> <li><strong>Question 1</strong></li> <li><strong>Question 4</strong></li> <li><strong>Question 7</strong></li> </ul> <p><strong>Section 2</strong></p> <ul> <li><strong>Question 2</strong></li> <li><strong>Question 9</strong></li> </ul> <p><strong>Section 3</strong></p> <ul> <li><strong>Question 3</strong></li> </ul>
 

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