0

I am working with 4 arrays, each array contains students last names and their test grades for 4 different tests. What I am trying to do is find the average For each student, calculate and display their average grade. How would I find Smiths average grade from all 4 arrays? I am very new to PHP so any directions would be useful. Thank you!

PHP:

$testOne = array (
        'Smith'=> 98,
        'Johnson' => 67,
   );

 $testTwo = array (
        'Smith'=> 100,
        'Johnson' => 85,
   );

 $testThree = array (
            'Smith'=> 78,
            'Johnson' => 92,
    );

   $testFour = array (
            'Smith'=> 91,
            'Johnson' => 88,
    );

I found one way to get the average but can anyone tell me if there is a more efficient way? I created separate arrays for each student and then divided by the count.

 $smith = array(98,100,91,75);
   $johnson = array(67,88,85,81)

   echo('Smiths average test score is ' . array_sum($smith) / 4);
CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49
  • Thank you Leo, so just create another array with each student as their own array? Or do you mean put all students and all test scores into one array? – CodeConnoisseur Jul 29 '18 at 18:01

4 Answers4

4
$TestResult = [
   'Smith' => ['98', '100', '78', '91'],
   'Johnson' => ['67', '85', '92', '88']
];

$Total = array_sum($TestResult['Smith']); // We are getting the array sum.
$Average = $Total/count($TestResult['Smith']); // the average is being calculated.

echo $Average;

https://3v4l.org/U3gCA

You can do this way.

Reference :

Özgür Can Karagöz
  • 1,039
  • 1
  • 13
  • 32
1

Suppose students have given four tests(as you said above) namely $testOne, $testTwo and $testThree and $testFour, then create an array like this,

$array = array('One', 'Two', 'Three', 'Four');

Declare a variable to capture total score,

$totalScore = 0;

Declare a student's name whose average score you want to calculate,

$candidate = 'Smith';

Use a foreach loop sum the candidate's total score of all subjects,

foreach($array as $a){
    $totalScore += ${'test'.$a}[$candidate];
}

Finally, calculate that candidate's average score like this,

$averageScore = $totalScore / count($array);

Here's the complete code:

$array = array('One', 'Two', 'Three', 'Four');
$totalScore = 0;
$candidate = 'Smith';
foreach($array as $a){
    $totalScore += ${'test'.$a}[$candidate];
}
$averageScore = $totalScore / count($array);

echo $averageScore;

Reference links:

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • One last question Rajdeep, I see where I declare which person I want to calculate the score for by declaring $candidate, would this same method be the most effective way if lets say there were 15 students in the class and I needed to find the average for all of them? – CodeConnoisseur Jul 29 '18 at 18:21
  • @WCA You mean average of all students or average of each student? – Rajdeep Paul Jul 29 '18 at 18:23
  • @Rajdeep, I apologize, I was not clear, yes the average for EACH student, so smiths average, johnsons average, Fields average etc. – CodeConnoisseur Jul 29 '18 at 18:25
  • @WCA In that case, declare `$candidate` as an array and use nested `foreach` loops to find each student's average, like this: [https://pastebin.com/pye83QAc](https://pastebin.com/pye83QAc). Edit: Here's the live example: https://eval.in/1042497 – Rajdeep Paul Jul 29 '18 at 18:31
0

if php 7+

$test[]['Smith'] = 98;
$test[]['Johnson'] = 67;

$test[]['Smith'] = 100;
$test[]['Johnson'] = 85;

$test[]['Smith'] = 78;
$test[]['Johnson'] = 92;

$test[]['Smith'] = 91;
$test[]['Johnson'] = 88;

then you can do the math

$avarage['Smith'] = array_sum(array_column($test, 'Smith'))/count($test);
$avarage['Johnson'] = array_sum(array_column($test, 'Johnson'))/count($test);

or even make a function

function avarage($name, $test)
{
   return array_sum(array_column($test, $name))/count($test);
}

calling function:

$avarage['Smith'] = avarage('Smith', $test);
$avarage['Johnson'] = avarage('Johnson', $test);
Leo Tahk
  • 420
  • 1
  • 6
  • 15
0

You can build a total array of all the arrays and loop one of them to get the student names.
Use the name in array_column to get the values from all the students scores and calculate the average.
Add the values to an associative array for convince.

$total = [$testOne, $testTwo, $testThree, $testFour];

foreach($total[0] as $name => $val){
    $averages[$name] = array_sum(array_column($total, $name))/count($total);
}
var_dump($averages);

Returns:

array(2) {
  ["Smith"]=>
  float(91.75)
  ["Johnson"]=>
  int(83)
}

If there had been more students this method would catch them all and just append the array.

https://3v4l.org/Lk1aL

Andreas
  • 23,610
  • 6
  • 30
  • 62