【问题标题】:PHP Multidimensional and Associative Arrays Column AveragePHP 多维和关联数组列平均值
【发布时间】:2021-08-07 08:34:03
【问题描述】:

大家好我正在为 PHP 做数组,列出所有列输出并获取测试分数的平均值,但我似乎无法弄清楚获取每列平均值的逻辑.但是我不太确定我这样做是对还是错,因为我的平均输出全为 0,而且我不知道如何更改它以读取值并进行计算。

如果您能提供帮助,我们将不胜感激。谢谢。

基本上,我希望输出是这样的;-

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

这是我的数组:-

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

这是我的代码:-

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

我使用函数并将其放在代码的末尾:-

<?php
    function average_scores($students, $i) {
    
         $total = 0;
         $students = array();
    
         foreach ($students as $student => $data) {
                       $total += $data[$i]; 
         }
         return $total / 4;
    }
?>
   

【问题讨论】:

    标签: php arrays multidimensional-array average associative-array


    【解决方案1】:

    既然你已经在循环了,试试这样

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

    最好的做法是将 HTML 和 PHP 代码分开。需要时使用 PHP 标记,它将提高代码的可读性 EG:

    echo "<b>". $students[$i]. " Average Scores</b>: ";
    // convert to
    <b><?php $students[$i]; ?> Average Scores</b>:
    

    【讨论】:

    • 哇,它可以工作,但为什么我不能在函数中调用它? @库鲁
    • 感谢@Kuru 的提示。对于如何改进我的代码的任何帮助,我都非常感谢,我将尝试练习分离我的代码。非常感谢
    • @NISHANAJIHAH 您可以使用该功能,但它将再次运行单独的循环(不必要)。关于您的功能,我认为您传递的参数有一些错误echo average_scores($students, $the_students); //$the_students is an array but you used that as a string inside the function
    • 是的,有一些错误我仍然需要练习,感谢@Kuru 的帮助
    【解决方案2】:

    尝试foreach 循环遍历学生获取 name 这是 key 并获取 student 这是关联数组,得到只有学生的grades值并将它们分解为变量,printf 以更易读的格式打印出来。

    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>";
    

    平均而言,您可以使用array_sum 将所有成绩相加,然后除以计数。

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

    【讨论】:

    • 我一直在尝试使用 array_sum 进行编码,但我总是遇到错误,因为我认为我实际检索值的方式。在从数组中检索值时,您有什么技巧吗?
    • 我确实测试了代码并比较了我编写的代码,这比我编写的代码干净得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    相关资源
    最近更新 更多