Page 1 of 1

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

Posted: Sat Nov 18, 2006 5:15 pm
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!

Posted: Sat Nov 18, 2006 5:19 pm
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.

Posted: Sat Nov 18, 2006 5:34 pm
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!

Posted: Sun Nov 19, 2006 1:15 am
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

Posted: Sun Nov 19, 2006 1:16 am
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.

Posted: Sun Nov 19, 2006 1:24 am
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();

Posted: Sun Nov 19, 2006 3:51 am
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

Posted: Sun Nov 19, 2006 6:56 am
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.