How to store a get variable into an array?

This seems like a rather silly question, but how do I store a $_GET variable in an array?

I have tried:

$bag = get_item();
		
$_SESSION['cart'] = array();
$_SESSION['cart'] = [$bag];

Open in new window


The problem with this is it only stores the current $_GET variable.  When you move to a different page and click a different product it overwrites the variable.  

So I want to put it in the array (store past $_GET variables) not have it be the only thing in the array.  Anyone know how to do this??
FairyBusinessAsked:
Who is Participating?
 
enachemcCommented:
$bag = get_item();
            
if(!isset($_SESSION['cart']))$_SESSION['cart'] = array();
array_push($_SESSION['cart'], $bag);
0
 
jason987Commented:
$_SESSION['cart'] = $_GET['item'];
0
 
FairyBusinessAuthor Commented:
Thanks :)
0
 
jason987Commented:
Of course you would want to track the number of items in the cart by doing something like setting $_SESSION['numitems'] so you know what array item you are working with.
0
 
FairyBusinessAuthor Commented:
hmmm ok that can be my next question jason if you want to answer it!
0
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.

All Courses

From novice to tech pro — start learning today.