php在多维数组中求和并得到平均值

时间:2014-09-11 14:08:37

标签: php html arrays

我从我的数据库中获取数据,这些数据一切正常,我得到了正确的行。但现在我需要对它们求和并计算平均值。

我目前的代码是:

$new = array();
                foreach($testquery as $key){
                    $new[$key['school']][$key['city_id']][] = array('points'=>$key['points']);
                }

我的数组结构如下:

    [Etelä-Tapiolan lukio] => Array
            (
                [4] => Array
                    (
                        [0] => Array
                            (
                                [points] => 5
                            )

                        [1] => Array
                            (
                                [points] => 5
                            )

                    )

            )


    [Oulunsalo lukio] => Array
            (
                [8] => Array
                    (
                        [0] => Array
                            (
                                [points] => 4
                            )

                        [1] => Array
                            (
                                [points] => 6
                            )

                        [2] => Array
                            (
                                [points] => 6
                            )

                        [3] => Array
                            (
                                [points] => 7
                            )

                    )

            )

  [Limingan lukio] => Array
        (
            [2] => Array
                (
                    [0] => Array
                        (
                            [points] => 4
                        )

                    [1] => Array
                        (
                            [points] => 5
                        )

                    [2] => Array
                        (
                            [points] => 3
                        )

                    [3] => Array
                        (
                            [points] => 3
                        )

                    [4] => Array
                        (
                            [points] => 4
                        )

                    [5] => Array
                        (
                            [points] => 6
                        )

                    [6] => Array
                        (
                            [points] => 4
                        )

                    [7] => Array
                        (
                            [points] => 6
                        )

                    [8] => Array
                        (
                            [points] => 3
                        )

                    [9] => Array
                        (
                            [points] => 3
                        )

                    [10] => Array
                        (
                            [points] => 5
                        )

                    [11] => Array
                        (
                            [points] => 7
                        )

                )

        )

)

阵列大小没有限制。现在我需要总结点,然后将总和除以每个数组的点数。

输出应为:

  • Etelä-Tapiolan lukio:5(解释5 + 5 = 10/2 = 5)

  • Oulunsalo lukio:5,75(解释4 + 6 + 6 + 7 = 23/4 = 5,75)

  • Limingan lukio:4.41(解释4 + 5 + 3 + 3 + 4 + 6 + 4 + 6 + 3 + 3 + 5 + 7 = 53/12 = 4.41)

我还需要ID = [4],[8],[2]数字

[Etelä-Tapiolan lukio] => Array
                (
                    [4] => Array
[Oulunsalo lukio] => Array
                (
                    [8] => Array
[Limingan lukio] => Array
                (
                    [2] => Array

最终输出将是:

<td class="4">Etelä-Tapiolan lukio:><span> 5 </span></td>
<td class="8">Oulunsalo lukio:><span> 5,75 </span></td>
<td class="2">Limingan lukio:><span> 4.41 </span></td>

1 个答案:

答案 0 :(得分:2)

通常情况下,你可以在这种情况下使用foreach。例如:

$total = array();
foreach ($new as $name_key => $stats) {
    foreach($stats as $class => $info) {
        $count = count($info); // get the count of individual points

        $sum = array_sum(array_map(function($piece){ // then, sum them
            return $piece['points']; // return each points
        }, $info));

        $total[$name_key][$class] = sprintf('%1.2f', $sum / $count); // normal average
    }
}

?>
<!-- present it on a table -->
<table>
    <tr>
    <?php foreach ($total as $name => $value): ?>
        <?php foreach ($value as $class => $ave): ?>
            <td class="<?php echo $class; ?>"><?php echo $name; ?><span><?php echo $ave; ?></span></td>
        <?php endforeach; ?>
    <?php endforeach; ?>
    </tr>
</table>

Sample Output