First time making a PHP/XML shopping cart and am having trouble with the update cart feature. I have a $quantities array that needs to update the $_SESSION[“cart”] values. Right now, the last quantity in the $quantities array is replacing all the $_SESSION[“cart”] values, as opposed to the first $quantities value replacing the first $_SESSION[“cart”] value, second replacing the second, etc. Below is a simple example showing my problem and the code I’m having trouble with.
print_r($SESSION["cart"]);
print_r($quantities);
foreach($quantities as $index=>$quantity)
{
foreach($_SESSION["cart"] as $key=>$value)
{
$newcart = str_replace($value, $quantity, $_SESSION["cart"]);
}
}
print_r($newcart);
which results in:
Array ( [Pizzas.Tomato & Cheese.Small] => 1 [Homemade Lasagna Ravioli or Manicotti.With Sausage.One Size] => 1 )
Array ( [0] => 3 [1] => 4 )
Array ( [Pizzas.Tomato & Cheese.Small] => 4 [Homemade Lasagna Ravioli or Manicotti.With Sausage.One Size] => 4 )
How can I get that last array ($newcart) to be
Array ( [Pizzas.Tomato & Cheese.Small] => 3 [Homemade Lasagna Ravioli or Manicotti.With Sausage.One Size] => 4 )
? Thanks.
Use
array_combine: