2

I'm using foreach loops to access records in a nested array:

while ($row = mysql_fetch_assoc($result)){  
    $test_groups[$row['group_name']][] = $row['lab_test'];
  }

  foreach($test_groups as $group_name => $tests){
    echo "<tr><td><span class='test_group_name'>" . $group_name . "</span></td></tr>";
    foreach($tests as $test){
        echo "<tr><td>" . $test . "</td></tr>";
    }
    echo "<tr><td>&nbsp;</td></tr>";
}
echo '</table>';

This works OK. Now I need to add another level to the nested array, like:

$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];

Would this work and how should I adjust the for each loops above to cater for this?

1 Answer 1

1

Assign to array should be with [] at the end

$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];

Foreach loop will be:

foreach ($departments as $dep_name => $groups) {
    echo $dep_name;

    foreach ($groups as $group_name => $tests) {
        echo $group_name;

        foreach ($tests as $test) {
            echo $test;
        }
    }  
}

It's just with echoes, make there HTML as you need.

Sign up to request clarification or add additional context in comments.

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.