1

I am very new at coding in PHP and I am trying to find out the averages mark of my students so they can view it on an online form. How would I find the averages for the following using a two dimensional array in PHP?

             Maths English Science
Student 1     50     92      62
Student 2     84     71      76
Student 3     67     87      68

I would need the average for:

  • average for student 1 across all subjects
  • average for student 2 across all subjects
  • average for student 3 across all subjects
  • average for maths for all 3 students
  • average for english for all three students
  • average for science for all three students

This is what I have so far:

<?php
$classMarks = array
(
'student 1' => array(50,92,62),
'student 2' => array(84,71,76),
'student 3' => array(67,87,68),
'maths' => array(50,84,67),
'english' => array(92,71,87),
'science' => array(62,76,68),
);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Zera
  • 15
  • 2
  • Related: [Calculating the Average of Values in a nested array object (PHP)](https://stackoverflow.com/a/48652356/2943403) and [PHP Average of subkeys in an array](https://stackoverflow.com/a/25129786/2943403) – mickmackusa Jan 04 '23 at 03:12

3 Answers3

2

No need to duplicate the data, you can just use:

$classMarks = array
(
'student 1' => array(50,92,62),
'student 2' => array(84,71,76),
'student 3' => array(67,87,68),
);

For the students, sum the particular student's scores, in this case student 1 and divide by the count of scores:

$student1 = array_sum($classMarks['student 1']) / count($classMarks['student 1']);

For the subjects, since maths is in the fist position (offset 0) then extract all values in offset 0, english would be offset 1, etc and compute the average the same way:

$maths   = array_sum($subj = array_column($classMarks, 0)) / count($subj);
$english = array_sum($subj = array_column($classMarks, 1)) / count($subj);

But a more meaningful structure might be:

$classMarks = array
(
'student 1' => array('maths'=>50,'english'=>92,'science'=>62),
'student 2' => array('maths'=>84,'english'=>71,'science'=>76),
'student 3' => array('maths'=>67,'english'=>87,'science'=>68),
);

Then access the subjects by their key such as maths:

$maths = array_sum($subj = array_column($classMarks, 'maths')) / count($subj);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • so for english it would be: $english = array_sum($d =array_column($classMarks, 'english')) / count ($d); ? – Zera May 02 '17 at 20:04
  • No, english would be offset `1` since it appears second in the array. I have edited and I would use the second structure that I propose so you just use `english`. – AbraCadaver May 02 '17 at 20:07
0

A foreach loop

Loop each element as a student key = > averages value Then add them, position 0 1 and 2, then divide by 3 (3 scores). I broke it up so you can see the steps, but it can be done within 1 line.

Then create the new array holding the averages

foreach($classMarks as $student => $averages){

$averaged = $averages[0] + $averages[1] + $averages[3];
$averaged = $average / count($classMarks($student);
$newArray[] = [$student = > averaged];
}

print_r($newArray);
clearshot66
  • 2,292
  • 1
  • 8
  • 17
0

If you get array with structure as in the question, you can calculate averages in such way

$avg = array();

foreach($classMarks as $k=>$v) {
  $avg[$k] = array_sum($classMarks[$k]) / count($classMarks[$k]);
}

print_r($avg);
splash58
  • 26,043
  • 3
  • 22
  • 34