-3

I have the following multi-dimensional array:

Student Name Exam Result Exam Grade
David 79 1H
Brian 54 2H2
Lisa 55 2H2
Simone 58 2H2
Francis 78 1H
Shauna 53 2H2
Sean 40 Pass
Xavier 95 1H
Tom 60 2H1
Andy 66 2H2

And I need to come up with write a function to count how many times each particular grade was achieved within the exam. For example 3 "1H's" were achieved. Any ideas?

$exam = array ( "names"  => array (  "David",
                                     "Brian",
                                     "Lisa",
                                     "Simone",
                                     "Francis",
                                     "Shauna",
                                     "Sean",
                                     "Xavier",
                                     "Tom",
                                       "Andy"
                                     ),
                  "results" => array ( 79,
                                       54,
                                       55,
                                       58,
                                       78,
                                       53,
                                       40,
                                       95,
                                       60,
                                       66
                                     ),
                    "grades" => array ( "1H",
                                       "2H2",
                                      "2H2",
                                       "2H2",
                                       "1H",
                                       "2H2",
                                       "Pass",
                                       "1H",
                                       "2H1",
                                       "2H1"
                                     ),
                );
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169

1 Answers1

0

Use array_count_values

 foreach(array_count_values($array['grades']) as $grade => $count){
     echo $grade.' achieved '.$count. ' times'.PHP_EOL;
 }
Wrikken
  • 69,272
  • 8
  • 97
  • 136