multi-dimension arrays in PHP

Paul Bryan paul at yahoo.co.uk
Thu Mar 6 21:06:26 EST 2003


On Thu, 6 Mar 2003 20:08, Paul Bryan wrote:
> This might also work (and requires one less variable):
>
> $_SESSION['sess_array'] = $myarray; # Note the different key name

After a couple of tests, I found this does work. I'm using PHP 4.1.2 so 
things may be different for you.

I set up 2 pages. The first (start.php) added the $myarray variable to the 
$_SESSION global array. Then it changed the value of that array (added some 
more items to it actually). The second page (show.php) started the session, 
then printed the contents of the $_SESSION variable.

First I viewed start.php, then show.php.

What I found was that if I did things the way you did originally (ie. setting 
$_SESSION['myarray'], the first time I viewed show.php things worked as you 
wanted them to. Then, if I viewed session.php, then show.php again, I got the 
same bug as you did. This is because when I viewed session.php the second 
time, $myarray was globally registered, so changing the value actually 
changed the variable registered with the session.

By giving the session variable a different name (sess_array as above), 
$myarray isn't registered globally so changing it makes no difference. ie. 
there is no link between $myarray and $_SESSION['sess_array']. $sess_array of 
course is registered globally.

The thing to note here is that adding the key 'myarray' to the $_SESSION 
variable didn't register $myarray globally. It was still seperate. It was 
only when I viewed session.php a second time that $myarray was registered 
globally <before> the script ran and hence any modifications to $myarray 
modified the session variable (also pointed to by $_SESSION['myarray']).

Hope that makes sense.

Paul Bryan.

p.s sorry about the late reply to the network monitoring discussion. The mail 
was frozen due to me turning my brain off. I never noticed that it didn't 
appear in the list. Today, I noticed the issue and forced all frozen messages 
out. 

It did explain why a mail or two disappeared...


More information about the linux mailing list