0

I have a multi dimensional array. To output all the values, is there an easier way than looping over each level like I do below?

$results['English']['Listening'][] = 100;
$results['English']['Listening'][] = 80;
$results['English']['Exam'][] = 70;
$results['English']['Exam'][] = 90;
$results['Computer']['Exam'][] = 90;

foreach ($results as $subject => $data) {
    foreach ($data as $component => $scores) {
        foreach ($scores as $score) {
            echo "$subject => $component => $score" . "<br/>";
        }
    }
}
4
  • 6
    looks fine to me Commented Apr 5, 2016 at 6:58
  • no easier way to do this, unless you want to restructure your array. :D Commented Apr 5, 2016 at 7:02
  • @mutokenji: Try this solution: stackoverflow.com/a/35453063/2156244 Commented Apr 5, 2016 at 7:10
  • no other way. @mutokenji Commented Apr 5, 2016 at 7:18

0

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.