Loop through array in PHP -
i have array i've created made of sections , questions. how can loop through sections , display nested questions of each section.
here how i'm creating array
$db = db_open(); $query = "select * assessment_selections assessment_id = '".$annual_assessment["id"]."' , selection = '1' order timestamp asc"; $result = db_query($db, $query); $result = db_fetch_all($result); if (!is_array) $result = array(); foreach($result $row) { $section[$row['section_id']][$row['question_id']] = $row; } here array
array ( [1] => array // section 1 ( [1] => array // question 1 ( [assessment_selection_id] => 70 [assessment_id] => 32 [section_id] => 1 [question_id] => 1 [selection] => 1 [timestamp] => 1368172762 ) ) [2] => array // section 2 ( [3] => array // question 3 ( [assessment_selection_id] => 68 [assessment_id] => 32 [section_id] => 2 [question_id] => 3 [selection] => 1 [timestamp] => 1368166250 ) ) [3] => array // section 3 ( [4] => array // question 4 ( [assessment_selection_id] => 69 [assessment_id] => 32 [section_id] => 3 [question_id] => 4 [selection] => 1 [timestamp] => 1368172690 ) ) [4] => array // section 4 ( [5] => array // question 5 ( [assessment_selection_id] => 71 [assessment_id] => 32 [section_id] => 4 [question_id] => 5 [selection] => 1 [timestamp] => 1368174153 ) ) )
expected results (how able echo them out in php)
section 1
- question 1
- question 4
- question 7
section 2
- question 2
- question 9
section 3
- question 3
you should use loop.
foreach($section $k=>$section) { echo "section $k"; foreach($section $i=>$question) { echo "question $i ".$question['assessment_id']; //more fields available here } }
Comments
Post a Comment