markfaye Posted May 2, 2017 Share Posted May 2, 2017 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: -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), ); Quote Link to comment Share on other sites More sharing options...
dalecosp Posted May 2, 2017 Share Posted May 2, 2017 Have you heard of array_sum(), then? Quote Link to comment Share on other sites More sharing options...
benanamen Posted May 2, 2017 Share Posted May 2, 2017 You need a little more than array_sum $average = array_sum($array) / count($array); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 3, 2017 Share Posted May 3, 2017 The array structure is bad, because you have duplicate data and rely on the order of the array values instead of using explicit student/subject pairs. So before you jump to any calculations, fix your data structure first. Where do you keep the marks? They should be in a database. Almost all servers today have a database system preinstalled (usually MySQL), so you just have to learn the basics of SQL. This will make things much easier than juggling with arrays. If you absolutely must keep the marks in a text file, do it properly. Store each piece of data only once, and be explicit: // a poor man's database; not recommended $classMarks = [ 'student 1' => [ 'maths' => 50, 'english' => 92, 'science' => 62, ], 'student 2' => [ 'maths' => 84, 'english' => 71, 'science' => 76, ], // ... ]; Quote Link to comment Share on other sites More sharing options...
benanamen Posted May 3, 2017 Share Posted May 3, 2017 (edited) If you use a database as @Jaques1 suggested, you can do this completely in SQL. You use FORMAT to specify the number of decimal places. SELECT id, student_name, math, english, science, sum(math + english + science) AS total, FORMAT(sum(math + english + science )/3,2) FROM students GROUP BY id Edited May 3, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 3, 2017 Share Posted May 3, 2017 SQL already has an AVG() function. The database table structure should look like this: student_id | subject_id | mark ------------+------------+------ 1 | 1 | 50 1 | 2 | 92 1 | 3 | 62 2 | 1 | 84 ... The average per student is simply SELECT student_id, AVG(mark) AS average_mark FROM marks GROUP BY student_id ; And the average per subject: SELECT subject_id, AVG(mark) AS average_mark FROM marks GROUP BY subject_id ; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.