naykidd Posted July 26, 2010 Share Posted July 26, 2010 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 Link to comment https://forums.phpfreaks.com/topic/208929-sum-of-values-in-a-multi-dimensional-array/ Share on other sites More sharing options...
AbraCadaver Posted July 26, 2010 Share Posted July 26, 2010 I'm not sure without seeing more code, but what about: $total = 0; while ($row1 = mysql_fetch_array($searchResult)) { $total += $row1['price']; } echo $total; Link to comment https://forums.phpfreaks.com/topic/208929-sum-of-values-in-a-multi-dimensional-array/#findComment-1091341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.