2

I am getting data from my database that's all working i get the right rows. But now i need to sum them and calculate the average.

My current code is:

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

My array structure is like this:

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

                )

        )

)

There is no limit on array sizes. Now i need to sum up the points and then divide the sum with the number of points each array has.

The output should be:

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

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

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

I also need the ID = [4],[8],[2] number

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

The final output is going to be:

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

2

Normally, you could just use a foreach in this case. Example:

$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

Sign up to request clarification or add additional context in comments.

Comments

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.