Learn how to a build a cloud-first strategyRegister Now


How to store a get variable into an array?

Posted on 2011-04-28
Medium Priority
Last Modified: 2012-05-11
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??
Question by:FairyBusiness
  • 2
  • 2

Expert Comment

ID: 35487785
$_SESSION['cart'] = $_GET['item'];
LVL 12

Accepted Solution

enachemc earned 2000 total points
ID: 35487793
$bag = get_item();
if(!isset($_SESSION['cart']))$_SESSION['cart'] = array();
array_push($_SESSION['cart'], $bag);

Author Closing Comment

ID: 35487807
Thanks :)

Expert Comment

ID: 35487828
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.

Author Comment

ID: 35487834
hmmm ok that can be my next question jason if you want to answer it!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question