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
    What do you want it to look like instead? Commented May 17, 2012 at 6:37

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

This is because of this line:

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

You essentially say in that line that 'items' key has product data, instead of key in items. It should be:

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

Keys -will- always occur, unless you want data to overwrite another. If you do not want keys (0,1 etc) then the only other option is merging data. In which case it would be:

$_SESSION['cart_items']['items']+=$product_data;

..but I don't think that is what you want.

Comments

0

You don't need the items, try the below way.

if (empty($_SESSION['cart_items'])) {
    $_SESSION['cart_items'] = array($product_data);
} else {
    $_SESSION['cart_items'][] = $product_data;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.