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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:06:46+00:00 2026-06-04T01:06:46+00:00

I have run into a problem that is a little irritating. Here is my

  • 0

I have run into a problem that is a little irritating. Here is my PHP code. Ignore where the variables are coming from. This is for shopping-cart functionality but it is applicable in many different areas.

$data_set = json_decode(stripslashes($_POST['varA']), true);
$pid = $pid['product_id'];
$quantity = $pid['quantity'];

$_SESSION['cartid'] = $_SESSION['cartid'] + 1;

$product_data = array("Product_ID" = > $pid, "quantity" = > $quantity, "cartid" = > $_SESSION['cartid']);

My issue is occurring at this place in the code. I first check to see if the Session variable has a value in it, if not then it proceeds to create an associative array.

if (empty($_SESSION['cart_items'])) {
    $_SESSION['cart_items'] = array("items" = > $product_data);
} else {
    array_push($_SESSION['cart_items']['items'], $product_data);
}


echo json_encode($_SESSION['cart_items']);

The end result after the first item is “added” looks like this:

{
    "items": {
        "Product_ID": "2",
        "quantity": "1",
        "cartid": 1
    }
}

However, after several the first add, every value gets a key:

{
    "items": {
        "0": {
            "Product_ID": "2",
            "quantity": "1",
            "cartid": 2
        },
        "1": {
            "Product_ID": "2",
            "quantity": "1",
            "cartid": 3
        },
        "Product_ID": "2",
        "quantity": "1",
        "cartid": 1
    }
}

How do I prevent these keys from occuring? Is this possible? If not, how can this be re-written so that the keys are added every time? And is this possible to parse and loop through in JS on the front end?

Sorry I have so many questions. Any help is really appreciated.

  • 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-04T01:06:48+00:00Added an answer on June 4, 2026 at 1:06 am

    In the first iteration, the $_SESSION['cart_items'] is empty, so you run this:

    $_SESSION['cart_items'] = array("items" => $product_data);
    

    This creates $_SESSION['cart_items']['items'] but you populate it with just the product by itself; you should define it as an array instead:

    $_SESSION['cart_items'] = array("items" => array($product_data));
    

    This creates an array with a single item which you can later extend with array_push.

    Having said that, you can replace the whole condition with just:

    $_SESSION['cart_items']['items'][] = $product_date;
    

    PHP will automatically create an empty array if it didn’t exist yet, followed by adding the product data as the next element.

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

Sidebar

Related Questions

I've run into an rather irritating problem in java. I have a program that
I have run into a rather weird little problem. In the following code I
I've just installed VS2008 and have run into a problem that I'm sure can
I frequently run into the problem that I have to preserve state between several
I have run into this problem a few times and I'm not happy with
When searching code for strings, I constantly run into the problem that I get
I'm currently developing a web application and have run into a little problem. I'm
One problem that I have frequently run into lately is the problem of my
Iam have run into a little problem with a partial projection. I have an
I have run into a problem trying to modify a form I myself have

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.