4

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.

Here is how I'm creating the array

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

Here is the 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 I would like to be able to echo them out in PHP)

Section 1

  • Question 1
  • Question 4
  • Question 7

Section 2

  • Question 2
  • Question 9

Section 3

  • Question 3
3
  • And what is the current (wrong) result with the code you've tried ? Please show us this code too Commented May 10, 2013 at 8:24
  • Pastebin your resultant array $result. Commented May 10, 2013 at 8:25
  • At least change if (!is_array) to if (!is_array($result)) Commented May 10, 2013 at 8:42

3 Answers 3

2

You should use this loop.

foreach($section as $k=>$section)
{
   echo "section $k";
   foreach($section as $i=>$question)
   {
     echo "question $i ".$question['assessment_id']; //more fields available here
   }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Just use foreach again:

foreach ( $data as $n => $sect) {
    echo "Section $n<br>";
    foreach ($sect as $q => $qdata) {
        echo " -> Question $q<br>";
        // ... do something
    }
}

Comments

1
foreach($section as $key => $value) {
    echo "Section ".$key;
    foreach($value as $key => $value) {
        echo "Question ".$key;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.