Jump to content

Working out averages using two dimensional arrays in PHP


markfaye

Recommended Posts

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),

    );

 

 

 

 

Link to comment
Share on other sites

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,
    ],
    // ...
];
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
;
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.