Bricktop Posted August 5, 2009 Share Posted August 5, 2009 Hi all, I have a foreach loop which extracts a price from an array and outputs it every time a match is found. The price is stored as $price. What I would like to do is add the $price variable together on each loop and store the number in $total. How would I achieve this please? My code is as follows: foreach($_POST['order_price'] as $value) { $prices = ""; foreach($domainprices as $tld=>$price) { if(substr($value, -strlen($tld)) == $tld) { $prices = $price; break; } } $content .='<div>'; $content .='<p><span>'.$value.'</span>'; $content .='<span>'.$price.'</span></p>'; $content .='</div>'; } The prices are all stored as; 12.99, 9.99, 14.99, 15.99 etc etc Many thanks! Link to comment https://forums.phpfreaks.com/topic/168929-solved-adding-numbers-in-array/ Share on other sites More sharing options...
bundyxc Posted August 5, 2009 Share Posted August 5, 2009 $total += $price; This will add the current value for $total to the current value for $price In essence, it's shorthand for: $total = $total + $price; Link to comment https://forums.phpfreaks.com/topic/168929-solved-adding-numbers-in-array/#findComment-891288 Share on other sites More sharing options...
Bricktop Posted August 5, 2009 Author Share Posted August 5, 2009 Perfect! Thanks bundyxc. Link to comment https://forums.phpfreaks.com/topic/168929-solved-adding-numbers-in-array/#findComment-891291 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.