How to store a get variable into an array?

Posted on 2011-04-28
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
    LVL 5

    Expert Comment

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

    Accepted Solution

    $bag = get_item();
    if(!isset($_SESSION['cart']))$_SESSION['cart'] = array();
    array_push($_SESSION['cart'], $bag);

    Author Closing Comment

    Thanks :)
    LVL 5

    Expert Comment

    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

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

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to count occurrences of each item in an array.
    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…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now