tags:

views:

38

answers:

1

Hello,

So i have a 2 dimensional array that is used over several pages (session)

            $_SESSION ["Table"][$_SESSION ["count"]] [0] = $filename;
            $_SESSION ["Table"][$_SESSION ["count"]] [1] = $size;
            $_SESSION ["Table"][$_SESSION ["count"]] [2] = $floor;
            $_SESSION ["Table"][$_SESSION ["count"]] [3] = $phone;
            $_SESSION ["Table"][$_SESSION ["count"]] [4] = $network;
            $_SESSION ["Table"][$_SESSION ["count"]] [5] = $totalprice;

This is used with a form so i can give in multiple input wich gets stored.

But my question is how exactly can i calculate the AVERAGE of $total price of all given in results?

Meaning for example i have 5 rows so this would mean 5 total prices. How exactly can i acces this value and count everything up / 5? This will happen in a other page so i would like to use sessions for this. /5 simply by count($_Session["table"]) , but really not sure about the other values.

Kind Regards.

+1  A: 

To iterate through an array, you can use foreach

$totalPrice = 0;
$totalItems = count( $_SESSION['Table'] );
foreach( $_SESSION['Table'] as $result ) {
    $totalPrice += $result[4];
}
echo $totalPrice / $totalItems;

Though i'm not quite getting your data structure - I have a feeling this might be a work around for a deeper problem.

Dan Heberden
+1 I share your sentiments about a possible 'deeper problem'.
BoltClock
Thanks a bunch finally got it to work, you're a lifesaver ;)
Neral