title: PHP Multidimensional and Associative Arrays Column Average
body: <p>Hi guys <strong>I'm doing arrays for PHP that list all the column output and get the average of test scores</strong> 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.</p>
  <p>Much appreciate it if you can help. Thank you.</p>
  <p><strong>Basically, I want the output to be like this;-</strong></p>
  <pre><code>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
  </code></pre>
  <p><strong>This is my array:-</strong></p>
  <pre><code>&lt;?php
   $students = array(
           &quot;RM&quot; =&gt; array(&quot;test1&quot; =&gt; 35, &quot;test2&quot; =&gt; 30,&quot;test3&quot; =&gt; 39, &quot;test4&quot; =&gt; 80),
           &quot;Justin&quot; =&gt; array(&quot;test1&quot; =&gt; 61, &quot;test2&quot; =&gt; 10,&quot;test3&quot; =&gt; 45, &quot;test4&quot; =&gt; 33),
           &quot;Miley&quot; =&gt; array(&quot;test1&quot; =&gt; 25, &quot;test2&quot; =&gt; 100,&quot;test3&quot; =&gt; 88, &quot;test4&quot; =&gt; 60),
      );
  ?&gt;
  </code></pre>
  <p><strong>This is my code:-</strong></p>
  <pre><code>&lt;table style=&quot;border: 1px solid black&quot;&gt;
    &lt;?php
          
        echo &quot;&lt;td&gt;&lt;p&gt;&lt;b&gt;Listing All Student Tests and Scores:-&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;&quot;;                  
        $the_students = array_keys($tudents);
        for($i = 0; $i &lt; count($students); $i++) {
            echo &quot;&lt;tr&gt;&quot;;
              
              // Output All Data
              echo &quot;&lt;td&gt;&lt;b&gt;&quot;. $the_students[$i] . &quot;&lt;/b&gt;&quot; . &quot; = &quot;;
                 foreach($students[$the_students[$i]] as $student =&gt; $score) {
                    echo &quot;&lt;b&gt;&quot;. $student .&quot;&lt;/b&gt;&quot;. &quot; : &quot; . $score. &quot;, &quot;;
                 }
          
            echo &quot;&lt;br&gt;&quot;;
          
            // Average Output                   
            echo &quot;&lt;b&gt;&quot;. $students[$i]. &quot; Average Scores&lt;/b&gt;: &quot;;
            if (array_key_exists($i, $the_students)) {
                echo average_scores($students, $the_students);
            }                      
                                      
            echo &quot;&lt;/td&gt;&quot;;
          echo &quot;&lt;/tr&gt;&quot;;
        }
    ?&gt;
  &lt;/table&gt;
  </code></pre>
  <p><strong>I use function and put it at the end of my code:-</strong></p>
  <pre><code>&lt;?php
      function average_scores($students, $i) {
      
           $total = 0;
           $students = array();
      
           foreach ($students as $student =&gt; $data) {
                         $total += $data[$i]; 
           }
           return $total / 4;
      }
  ?&gt;
     
  </code></pre>