Jump to content

Sum of values in a multi dimensional array


naykidd

Recommended Posts

I'm trying to work out the sum of a set of values (named price in my table) in a multi dimensional array, an array generated from search results ($searchResults).

 

When i echo the contents of this array (or what i assume is an array), or try to use the 2 examples at http://www.phpfreaks.com/forums/index.php/topic,215109.0.html i get the following

 

"array_reduce() [function.array-reduce]: the first argument should be an array in..... etc etc"

 

Here is the code I use

 

function multi_array_sum($total, $next) {
   $total += $next['price'];
   return $total;
}
$totalCost = array_reduce($searchResult, 'multi_array_sum');

echo $totalCost

 

I also have tried $row, $row1 etc which wont work as it isn't filtered through my search results, but i tested it to see the output (row is used to print my results, i use

 

while ($row1 = mysql_fetch_array($searchResult)) { 
echo $row1['price'] }

)

 

 

is there a function i should be using that will work with $searchResult to get the price and add it up?

thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.