PHP,循环中的平均数

时间:2017-05-09 03:25:08

标签: php html forms math html-table

我目前在一个表中有一个循环,它输出8个配方值作为一个跨越8列的表行。这是它的截图:

enter image description here

它的方式很棒,除了在“总体”下校正精度的最后一个列单元格。这需要是前面8个值的平均值(显然,一个是0或NAN)。

以下是创建最后一行的代码:

        <tr>
        <td style="border:none; text-align: left;">Meter Volume: </td>
        <td><? echo $row['test1MeterVol'];?>&nbsp;</td>
        <td><? echo $row['test2MeterVol'];?>&nbsp;</td>
        <td><? echo $row['test3MeterVol'];?>&nbsp;</td>
        <td><? echo $row['test4MeterVol'];?>&nbsp;</td>
        <td><? echo $row['test5MeterVol'];?>&nbsp;</td>
        <td><? echo $row['test6MeterVol'];?>&nbsp;</td>
        <td><? echo $row['test7MeterVol'];?>&nbsp;</td>
        <td><? echo $row['test8MeterVol'];?>&nbsp;</td>
    </tr>
    <tr>
        <td style="border:none; text-align: left;">Tester Volume: </td>
        <td><? echo $row['test1TesterVol'];?>&nbsp;</td>
        <td><? echo $row['test2TesterVol'];?>&nbsp;</td>
        <td><? echo $row['test3TesterVol'];?>&nbsp;</td>
        <td><? echo $row['test4TesterVol'];?>&nbsp;</td>
        <td><? echo $row['test5TesterVol'];?>&nbsp;</td>
        <td><? echo $row['test6TesterVol'];?>&nbsp;</td>
        <td><? echo $row['test7TesterVol'];?>&nbsp;</td>
        <td><? echo $row['test8TesterVol'];?>&nbsp;</td>
    </tr>
    <tr>
        <td style="border:none; text-align: left;">Tester Accuracy: </td>
        <td><? echo $row['test1Accuracy'];?>%&nbsp;</td>
        <td><? echo $row['test2Accuracy'];?>%&nbsp;</td>
        <td><? echo $row['test3Accuracy'];?>%&nbsp;</td>
        <td><? echo $row['test4Accuracy'];?>%&nbsp;</td>
        <td><? echo $row['test5Accuracy'];?>%&nbsp;</td>
        <td><? echo $row['test6Accuracy'];?>%&nbsp;</td>
        <td><? echo $row['test7Accuracy'];?>%&nbsp;</td>
        <td><? echo $row['test8Accuracy'];?>%&nbsp;</td>
        <td style="border:none;">Overall</td>
    </tr>
    <tr>

        <td style="border:none; text-align: left;">Corrected Accuracy: </td>

/*This is the loop that builds the formula for all 8 tests*/

        <?php for($i=1; $i<=8; $i++){ 
            $testFormA = $row["test".$i."MeterVol"] / $row["test".$i."TesterVol"]; 
            $testFormB = $testFormA * $row["test".$i."Accuracy"]; 
            $testFinalForm = $testFormB / 100;                                      
        ?>
        <td><?php echo round($testFinalForm,3) ?>%</td>


        <?php }?>

由于这8个值是在循环中收集并放在一行中的,有没有办法将它们的平均值放在最后一个单元格中?

更新:

var_dump($row["test".$i."MeterVol"]  );
var_dump($row["test".$i."TesterVol"]);

string(4) "0.61" string(4) "0.62" string(4) "0.98" string(1) "1" string(4) "2.41" string(4) "2.44" string(4) "8.87" string(4) "9.03" string(4) "6.65" string(4) "6.72" string(5) "26.12" string(4) "26.5" string(5) "52.45" string(4) "52.6" string(1) "0" string(1) "0"

1 个答案:

答案 0 :(得分:1)

要计算8个值的平均值,您需要将8个值相加,然后除以值的数量,在本例中为8。

例如:

<?php

$accuracy = [];

for ($i = 1; $i <= 8; $i++) {
    $accuracy[] = $row["test{$i}MeterVol"] / $row["test{$i}TesterVol"] * $row["test{$i}Accuracy"] / 100;
}

$average = array_sum($accuracy) / count($accuracy);

如果您在数组中的当前循环的每次迭代期间存储$testFinalForm的值,则根据上面的示例,您可以使用array_sum计算总和,从而计算平均值使用count查找数组长度。