Hi guys I'm doing arrays for PHP that list all the column output and get the average of test scores but I can't seem to figure out the logic to get the average of each column. But I'm not too sure if I'm doing it right or wrong because my output for average is all 0 and I don't know how to change it to read the value and make it calculate.
Much appreciate it if you can help. Thank you.
Basically, I want the output to be like this;-
RM = Physics : 35, Maths : 30, Chemistry : 39, English : 80,
RM Average Scores: 46
Justin = Physics : 61, Maths : 10, Chemistry : 45, English : 33,
Justin Average Scores: 37.25
Miley = Physics : 25, Maths : 100, Chemistry : 88, English : 60,
Miley Average Scores: 68.25
This is my array:-
<?php
$students = array(
"RM" => array("test1" => 35, "test2" => 30,"test3" => 39, "test4" => 80),
"Justin" => array("test1" => 61, "test2" => 10,"test3" => 45, "test4" => 33),
"Miley" => array("test1" => 25, "test2" => 100,"test3" => 88, "test4" => 60),
);
?>
This is my code:-
<table style="border: 1px solid black">
<?php
echo "<td><p><b>Listing All Student Tests and Scores:-</b></p></td>";
$the_students = array_keys($tudents);
for($i = 0; $i < count($students); $i++) {
echo "<tr>";
// Output All Data
echo "<td><b>". $the_students[$i] . "</b>" . " = ";
foreach($students[$the_students[$i]] as $student => $score) {
echo "<b>". $student ."</b>". " : " . $score. ", ";
}
echo "<br>";
// Average Output
echo "<b>". $students[$i]. " Average Scores</b>: ";
if (array_key_exists($i, $the_students)) {
echo average_scores($students, $the_students);
}
echo "</td>";
echo "</tr>";
}
?>
</table>
I use function and put it at the end of my code:-
<?php
function average_scores($students, $i) {
$total = 0;
$students = array();
foreach ($students as $student => $data) {
$total += $data[$i];
}
return $total / 4;
}
?>
My Answer:
There are a few issues in your code that need to be addressed in order to calculate the average scores correctly. Here are the corrections you need to make:
1. In the foreach loop where you are calculating the average scores, you are reassigning the $students variable inside the loop. This will cause issues with the calculation. You should use a different variable name for the array inside the loop.
2. In the average_scores function, you are passing $i as the index of the column. However, in the foreach loop, $data is the array of test scores for each student. You should access the test score for the specific column using the $i key.
Here is the corrected code:
php
$students = array(
"RM" => array("test1" => 35, "test2" => 30, "test3" => 39, "test4" => 80),
"Justin" => array("test1" => 61, "test2" => 10, "test3" => 45, "test4" => 33),
"Miley" => array("test1" => 25, "test2" => 100, "test3" => 88, "test4" => 60),
);
function average_scores($students, $i) {
$total = 0;
foreach ($students as $student => $data) {
$total += $data[$i];
}
return $total / count($students);
}
?>
echo "Listing All Student Tests and Scores:-
";
$the_students = array_keys($students);
for ($i = 0; $i < count($students); $i++) {
echo "";
echo "" . $the_students[$i] . " = ";
foreach ($students[$the_students[$i]] as $student => $score) {
echo "" . $student . " : " . $score . ", ";
}
echo "
";
echo "" . $the_students[$i] . " Average Scores: ";
echo average_scores($students, $i);
echo " ";
echo " ";
}
?>
With these corrections, your code should now correctly calculate and display the average scores for each column.
Rate this post
4 of 5 based on 1078 votesComments