| re: Summing specific values in a cookie array (newbie)
You are not using array_sum correctly (in fact it does not apply to the job you are trying to do).
array_sum sums the values in an array, however what you want to do is sum the values in a given index of several different arrays.
What documentation are you using as a reference?
This is the sort of code you want
[php]
<?
$total = 0;
foreach ($_COOKIE[mycookie] as $valueq)
{
$value3 = unserialize(stripslashes($valueq));
$total += $value3[9];
}
echo "Total Cost = " . $total . "\n";
?>
[/php]
BTW if in $_COOKIE[mycookie] mycookie is a string array index then it is good form to put quotes round it like so $_COOKIE['mycookie']. This protects the array access if case mycookie is accidentally defined as so
[php]
$_COOKIE[mycookie] = 5;
define ( 'mycookie', 2 )
echo $_COOKIE[mycookie]; /* Statement 1*/
echo $_COOKIE['mycookie']; /* Statement 2*/
[/php]
In this code example statement 1 will give an undefined variable warning because it actually trys to access $_COOKIE[2], statement 2 will work correctly display the value 5.
|