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

  • SEARCH
  • Home
  • 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 9194045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:18:38+00:00 2026-06-17T21:18:38+00:00

I have a simply cart array, that I want to update if the user

  • 0

I have a simply cart array, that I want to update if the user selects the same product again. If they already have 5 pencils in their cart, and they add to cart 4 more pencils, I want them to have 9 pencils in their cart, not two entries for pencils of 5 and 4.

The code I have this:

$sub = array("id" => $_POST['pID'], "name" => $_POST['pName'], "price" => $_POST['pPrice'], "quantity" => (int) $_POST['pQuant']);
$updated = false;
foreach($cart as $c)
{
    if($c['id']==$_POST['pID']) {
        var_dump($c); 
        echo "<br>";

        $c['quantity'] += (int) $_POST['pQuant'];
        var_dump($c); 
        echo "<br>";
        var_dump($cart); 
        echo "<br>";
        $updated = true;
    }

}

if(!$updated) {$cart[] = $sub;}

For some stupid reason, (Im sure it’s a really obvious thing i’m missing here), the first var_dump of $c is fine. That’s the original values. Then the second var_dump, has the number added onto it. However on the third var_dump, of $cart, The value hasn’t changed?

I don’t get why.

  • 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-06-17T21:18:40+00:00Added an answer on June 17, 2026 at 9:18 pm

    Within the foreach the variable $c is only a copy of the basket item, so you update the copy, but not the original item within $cart. This can be circumvented by using a reference for $c instead:

    foreach($cart as &$c)
    

    Notice the & in front of $c.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simply subclass of UIButton that will fire off a custom
We have several web applications that create a shopping cart, save it to a
We have some shopping cart pages which work with both guest and user paths.
I have to create a (virtual, simple )product and then add to cart both
I have this issue that I ran into today. I am making a mini-cart
I have a simple shopping cart application that displays the session's shopping cart with
I have a simple shopping cart web site that uses a MySQL database to
I'm writing a simple shopping cart application and have hit a road block. Currently
In my first approach with Rails I have simply create a void SayController and
I simply have 2 tabs and used Experience - Multiple Android Activities in a

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.