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

I am building a simple shop cart and I am getting the following array
I am building simple blogging framework using PHP and MySQL, and on the add
I am building a simple Firefox extension using the Add-on SDK 1.0. Scouring the
I'm building a simple java Servlet which passes categories using a URL variable into
I'm building simple dictionary application using WPF. I'm using MVVM pattern, databinding and FlowDocument
I'm building a simple chat server in java , where users can have private
I'm building an ASP.Net website. I have a cart class which stores the items
If you are building a simple website using just simple HTML/CSS/Javascript that has say
I am currently building a simple application for Android using the NDK and needs
I am building a simple client-server program , I have in main : FTPClient

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.