multi-dimension arrays in PHP

Paul Bryan paul at
Thu Mar 6 20:08:41 EST 2003

On Thu, 6 Mar 2003 18:02, Robert Edwards wrote:
> Not really a Linux question, per say, but it is running on a Linux-based
> server :-)
> I have a multi-dimensioned array that I am saving in the super-global
> session variable, $_SESSION, thus:
> $myarray[$x][$y][$z] = "some string";
> $_SESSION['myarray'] = $myarray;
> From the documentation on arrays in the online PHP manual it states that
> assigning arrays like this will involve value copying:
> "You should be aware, that array assignment always involves value copying.
> You need to use the reference operator to copy an array by reference."
> I add other values to $myarray, but I don't expect these to be saved in the
> session.
> When the next page loads, I do this:
> $myarray = $_SESSION['myarray'];
> and now $myarray does contain the extra added values, which implies that
> they were also saved into the session store. What I really want is to only
> see the original stuff that was saved in $myarray before I saved it in the
> session.
> Anyone know why this might be happening, how to avoid it, or where I should
> ask for more help?

I think you might find this is related to the register_globals configuration 
option in your php.ini file. What I think is happening is that when you add 
the key 'myarray' to the $_SESSION array, the variable $myarray is being 
registered globally. In other words, $myarray points to the same object as 

register_globals is a convinience that should generally be avoided. See for more information.

If I'm right, you can turn register_globals off in your php.ini. Note that 
this could quite easily break any code already written. Make sure you read 
about register_globals first. There's more info at

If I'm wrong, or turning register_globals off isn't an option (I'm stuck with 
a server like that because of a lot of code that breaks with register_globals 
off), you could always try:

$sess_array = $myarray; # They should be two seperate variables now
$_SESSION['sess_array'] = $sess_array.; # Now you can change $myarray

This might also work (and requires one less variable):

$_SESSION['sess_array'] = $myarray; # Note the different key name

Otherwise, there's info about the mailing lists here

Good luck,

More information about the linux mailing list