0

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;
    }
?>
   

2 Answers 2

1

Since you are already looping, try like this

<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++) {
         $total = 0;
          echo "<tr>";
            
            // Output All Data
            echo "<td><b>". $the_students[$i] . "</b>" . " = ";
               foreach($students[$the_students[$i]] as $student => $score) {
                  $total += $score;
                  echo "<b>". $student ."</b>". " : " . $score. ", ";
               }
        
          echo "<br>";
        
          // Average Output                   
          echo "<b>". $students[$i]. " Average Scores</b>: ";
          echo $total/ count($students[$the_students[$i]]);                      
                                    
          echo "</td>";
        echo "</tr>";
      }
  ?>
</table>

The best practice would be to keep HTML and PHP code separate. Use the PHP marking whenever needed, it will improve the code readability EG:

echo "<b>". $students[$i]. " Average Scores</b>: ";
// convert to
<b><?php $students[$i]; ?> Average Scores</b>:
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, it works but why can't I call it in a function? @Kuru
Thank you for the tips @Kuru. I'm really grateful for any help with how I can improve my code too and I will try to practice to separate my code. Thank you so much
@NISHANAJIHAH you can use the function, but it's going to run a separate loop again (unnecessary). Regarding your function, I think the parameters you are passing have some bug echo average_scores($students, $the_students); //$the_students is an array but you used that as a string inside the function
Yeah, there is some bug I still need practice for this and thank @Kuru for the help
1

Try foreach to loop through students get the name which is the key and get also the student which is associative array, get only the values grades of student and destruct them into variables, printf to print them out in more readable format.

echo '<table style="border: 1px solid black">';
echo "<tr><th>Listing All Student Tests and Scores:-</th></tr>";
foreach ($students as $name => $student) {
    $grades = array_values($student);
    [$Physics, $Maths, $Chemistry, $English] = $grades;
    echo "<tr><td>";
    printf("%s = Physics : %d, Maths : %d, Chemistry : %d, English : %d<br>
            %s Average Scores: %.2f"
            ,$name, $Physics, $Maths, $Chemistry, $English
            ,$name, average_scores($grades));
    echo "</td></tr>";
}
echo "</table>";

For average you can use array_sum to sum all grades and divide them by their count.

function average_scores($grades) {
    return array_sum($grades)/count($grades);
}

2 Comments

I've been trying to code with array_sum but I end up with errors all the time because I think the way I actually retrieve the value. Do you have any tips when it comes to retrieving value from an array?
I did test the code and compare the one that I code this is much cleaner than the one I did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.