brinkley Posted February 28, 2010 Share Posted February 28, 2010 Hello, hoping someone could help - I've been on this a few days and can't figure this out for the life of me. I have an array, and have to sort through it with list() and each() , and then I will have to do some calculations on the values, but I can't seem to figure out how to do so. Here is the array and the loop that I have, but how do I separate the price and shipping to add/multiply and perform math/calculations on those values? $product = array( array ( "name" => "DVD", "price" => 12.95, "shipping_price" => 0), array ( "name" => "Blu-ray Player", "price" => 99.5, "shipping_price" => 0.10), array ( "name" => "DVD Player", "price" => 42.99, "shipping_price" => 0.1)); while (list($key, $val) = each($product)) { echo "$key => $val\n"; } Thank you in advance Link to comment https://forums.phpfreaks.com/topic/193631-arrays-and-list-and-math/ Share on other sites More sharing options...
trq Posted February 28, 2010 Share Posted February 28, 2010 You would be better off not using list. Example.... $products = array( array ( "name" => "DVD", "price" => 12.95, "shipping_price" => 0), array ( "name" => "Blu-ray Player", "price" => 99.5, "shipping_price" => 0.10), array ( "name" => "DVD Player", "price" => 42.99, "shipping_price" => 0.1) ); foreach ($products as $product) { $total = $product['price'] + $product['shipping_price']; echo "The {$product['name']} will cost you \$ $total in total"; } Link to comment https://forums.phpfreaks.com/topic/193631-arrays-and-list-and-math/#findComment-1019264 Share on other sites More sharing options...
brinkley Posted February 28, 2010 Author Share Posted February 28, 2010 Thanks for the reply. That's what I was thinking as well, but the instructor is insisting we try using list() and each() with this example Link to comment https://forums.phpfreaks.com/topic/193631-arrays-and-list-and-math/#findComment-1019268 Share on other sites More sharing options...
trq Posted February 28, 2010 Share Posted February 28, 2010 Ok, then.... while (list($name, $price, $shipping) = each($product)) { $total = $price + $shipping; echo "The {$product['name']} will cost you \$ $total in total"; } Link to comment https://forums.phpfreaks.com/topic/193631-arrays-and-list-and-math/#findComment-1019274 Share on other sites More sharing options...
brinkley Posted February 28, 2010 Author Share Posted February 28, 2010 Got it now. I understand, thanks! Link to comment https://forums.phpfreaks.com/topic/193631-arrays-and-list-and-math/#findComment-1019383 Share on other sites More sharing options...
teamatomic Posted February 28, 2010 Share Posted February 28, 2010 Shouldnt that be something more like this? while (list($key,$value) = each($product)) { $total=$value['price']+$value['shipping_price']; echo "The item {$value['name']} costs a total of \$$total<br>"; } HTH Teamtomic Link to comment https://forums.phpfreaks.com/topic/193631-arrays-and-list-and-math/#findComment-1019435 Share on other sites More sharing options...
brinkley Posted March 1, 2010 Author Share Posted March 1, 2010 I took what you did and that really helped enormously. Thank you! Link to comment https://forums.phpfreaks.com/topic/193631-arrays-and-list-and-math/#findComment-1019670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.