Click to See Complete Forum and Search --> : sum of array values


jrthor2
11-12-2007, 07:38 AM
If I have an array that looks like this:

Array ( [0] => Array ( [0] => ggp_2 [1] => Test 3 notes2 [2] => 0.36 [3] => 800 [4] => 288 [5] => ) [1] => Array ( [0] => ggp_3 [1] => Test 3a [2] => 0.25 [3] => 300 [4] => 75 [5] => ) )

I want to take the 4th value in each array and add them up to get a grand total, then add that total to my array so I can read it in my page? Here is the code I'm using to create my array:

$num_products=mysql_numrows($result);
for ($i = 0; $i < $num_products; $i++) {
$id = mysql_result($result,$i,"id");
$name = mysql_result($result,$i,"name");
$price = mysql_result($result,$i,"price");
$min_qty = mysql_result($result,$i,"min_qty");
$cookies_sorted = array();
if(preg_match_all('/\b(\d+):(\d+)\b/', $ggp_orderlist, $matches))
{
foreach($matches[1] as $k => $v)
{
$cookies_sorted[$v] = $matches[2][$k];
if ($v = $id) {
$qtyOrdered = $cookies_sorted[$v];
$qtyPrice = $price*$qtyOrdered;
}
}
}

$products[] = array("ggp_".$id,$name,$price,$qtyOrdered,$qtyPrice,$qtyTotal);
//array_push($products,$name,$price);
}

thanks.

scragar
11-12-2007, 07:46 AM
$total = 0;
foreach($products as $val){
$total += $val[4];
};
echo $total;

jrthor2
11-12-2007, 08:54 AM
Thanks, I got it working.