开发者

PHP 2d array question

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 tot开发者_运维问答al 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜