• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

PHP shopping carts and Key Value Pairs

I am attempting to create a PHP shopping cart using a session. Below is the code I have so far for adding to the cart.

$cart = $_SESSION['cart'];
$cart[sizeof($cart)+1] = $_GET['pid'] ;
$_SESSION['cart'] = $cart;

I want to store the quantity ordered as a key value pair in the array. I have tried this using the below code, without any luck. How do I do this?

$cart[sizeof($cart)+1] = ($_GET['pid'] => $_GET['quantity']);

Many Thanks
1 Solution
Rather than creating the intermediary variable, you can create the new key directly in the cart $_SESSION array:

$_SESSION['cart'][$_GET['pid']] = $_GET['quantity'];
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now