Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:19:41+00:00 2026-05-27T22:19:41+00:00

First time making a PHP/XML shopping cart and am having trouble with the update

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:19:42+00:00Added an answer on May 27, 2026 at 10:19 pm

    Use array_combine:

    $newcart=array_combine(array_keys($_SESSION['cart']),$quantities);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i'm having trouble with making like a simple waiting time between 2 php
This is my first time making an app with XML layouts. In the past
This is my first time making a hash table. I'm trying to associate strings
first time SO user :) I know that I can format a number like
first time use JTree. Just wondering is it possible to have more than one
First time Stack Overflow poster. Please bear with me! :) I have a set
I have spent quite some time making a function and the last 15-20 minutes
I have just made my first proper little desktop GUI application that basically wraps
I'm making a call to a PHP script that returns a random element from
long time listener. First time caller... Not strictly a PHP question as it involves

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.