$_SESSION[''][''] Possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

$_SESSION[''][''] Possible?

Post by rxsid »

Hi all,

What I'm trying to do is something like this, of course it doesn't work...I'm just not sure if it's possible to create (and reference) session arrays in this manor or if i've got a syntax error:

Code: Select all

$_SESSION['cart']['itemNumber'] = $itemNumber;
$_SESSION['cart']['itemQty'] = $itemQty;
$_SESSION['cart']['itemDesc'] = $itemDesc;
$_SESSION['cart']['itemPrice'] = $itemPrice;
//...etc
the error I get is:
Cannot use a scalar value as an array
thanks!
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

That should work just fine. Maybe it's something with the values of your variables. Try storing some value instead of $var and see what happens.
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

Post by rxsid »

yep, your right. I wound up having to unset() the ['cart'] session to clear it out because I had done some different testing (without the [''][''] method). thanks for the reply wtf!
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

scalar values works in super global SESSION variables like a normal array.

Code: Select all

<?php
session_start();

$_SESSION["x"]["y"] = "1";
$_SESSION["x"]["z"] = "2";

print_r($_SESSION);
?>

Code: Select all

Array
(
    [x] => Array
        (
            [y] => 1
            [z] => 2
        )
)


Cheers,
Dibyendra
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

np...
webdev extension for firefox has really handy option to clear session variables which I have found very handy when working with sessions. I highly recommend it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It pays to initialize variables. In this instance having the following before the assignments would have avoided the issue.

Code: Select all

$_SESSION["x"] = array();
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

feyd wrote:It pays to initialize variables. In this instance having the following before the assignments would have avoided the issue.

Code: Select all

$_SESSION["x"] = array();
I didn't understand feyd what you are talking about. Please make your comments clearer.

Dibyendra
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

dibyendrah wrote:
feyd wrote:It pays to initialize variables. In this instance having the following before the assignments would have avoided the issue.

Code: Select all

$_SESSION["x"] = array();
I didn't understand feyd what you are talking about. Please make your comments clearer.
$_SESSION["x"]["y"] means _SESSION is an array and it has an index/element _SESSION["x"] and it's an array and has an index/element _SESSION["x"]["y"]
_SESSION already is an array (php initializes it automagically), $_SESSION["x"] is not (by default).
$_SESSION["x"] = array() makes it an array.
Post Reply