Jump to content

Arrays and list() and math


brinkley

Recommended Posts

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

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";
}

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.