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 6203507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:55:46+00:00 2026-05-24T04:55:46+00:00

I am building a simple cart for a friend and using an array in

  • 0

I am building a simple cart for a friend and using an array in session to store it.

To add an item to the cart I have this code

$next_item = sizeof($_SESSION['cart']) +1;
$_SESSION['cart'][$next_item] = array(item => $product_id, option => $option, qty => 1);

What I’m struggling with how to update an item’s quantity in this array should someone add another of the same item or update the cart. Can anyone point me in the right direction? 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-24T04:55:46+00:00Added an answer on May 24, 2026 at 4:55 am

    Something like

    foreach($_SESSION['cart'] as $key => $value) {
    
        if ($_SESSION['cart'][$key]['item'] == $product_id) {
    
            $_SESSION['cart'][$key]['qty'] += $qty_to_add;
        }
    }
    

    I would change the structure of your array.

    Instead of

    $_SESSION['cart'] = array(
        1 => array(
            'item' => 1,
            'option' => 1,
            'qty' => 1),
        2 => array(
            'item' => 2,
            'option' => 1,
            'qty' => 1),
        3 => array(
            'item' => 3,
            'option' => 1,
            'qty' => 1)
    );
    

    Use

    $_SESSION['cart'] = array(
        1 => array(
            'option' => 1,
            'qty' => 1),
        2 => array(
            'option' => 1,
            'qty' => 1),
        3 => array(
            'option' => 1,
            'qty' => 1)
    );
    

    Where the key is the product id. It will make referencing the items easier and you can update the quantity in one line

    $_SESSION['cart'][$product_id]['qty'] += $qty_to_add;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im building a simple web app in Python using web.py - and was wondering
I'm building a simple form mailer in symfony using sfExtraForm plugin captcha. Everything works
I'm building a simple html5 app to monitor the total network traffic that's incoming
I am building a simple ASP.NET MVC site to record and track donations. We
I'm building a simple image gallery with jquery but running into an annoying problem.
I'm building a simple UserControl , DoubleDatePicker , which defines a DependencyProperty , SelectedDate
I'm building a simple email functionality in my web application and would like to
I am building a simple application that listens to NetworkAvailibilityChanged Event. It's all good,
I'm building a simple random div displayer. I want to display one random li
I am building a simple CMS and need a good solution for an input

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.