etm124
etm124

Reputation: 2140

Pushing an array to a session variable without re-instantiating the variable

I'm creating an $item array that I'd like to push to $_SESSION['cart']. After constructing my item array, I am trying:

$_SESSION['cart'][] = $item[$item_id];

Referencing: Can I use array_push on a SESSION array in php?

However, my cart session variable keeps getting overwritten, rather than added to. Any other suggestions?

As requested from Mark:

First time being run:

Notice: Undefined variable: _SESSION in C:\inetpub\wwwroot\domain\store\cart.php on line 5
NULL array(3) { ["title"]=> string(37) "PA State and Federal Laminated Poster" ["price"]=> string(5) "55.95" ["qty"]=> string(1) "3" }

Second time:

Notice: Undefined variable: _SESSION in C:\inetpub\wwwroot\domain\store\cart.php on line 5
NULL array(3) { ["title"]=> string(53) "PA State and Federal Laminated Poster SPANISH Edition" ["price"]=> string(5) "55.95" ["qty"]=> string(1) "1" }

Upvotes: 0

Views: 156

Answers (1)

Matt
Matt

Reputation: 7040

You have to call session_start() before you reference $_SESSION. The output you posted indicates you don't do that.

Notice: Undefined variable: _SESSION in C:\inetpub\wwwroot\domain\store\cart.php on line 5

Upvotes: 1

Related Questions