By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

copying a multidimensional array to $_SESSION

P: n/a
Probably a simple question but I can't find the answer anyway.

Specifically, is it possible to copy a multidimensional array into the
$_SESSION array - ie a deep clone of all keys and data?

I naively assumed that

$_SESSION["myArray"'] = $myArray ;

would work but it doesn't appear to work. Is there a single function
or assignment I can use, or would I need to use a "foreach" at every
level?
---
Rob Tweed
M/Gateway Developments Ltd

Global DOMination with eXtc : http://www.mgateway.tzo.com
---
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Rob Tweed <rt****@blueyonder.co.uk> wrote in message news:<k2********************************@4ax.com>. ..
Probably a simple question but I can't find the answer anyway.

Specifically, is it possible to copy a multidimensional array into the
$_SESSION array - ie a deep clone of all keys and data?
Global DOMination with eXtc : http://www.mgateway.tzo.com


Example:
$_SESSION['string1'] = 'String1';
$_SESSION['string2'] = 'String2';
$_SESSION['array1'] = array(1,2,3,4,5);
$_SESSION['array2'] = array(11,12,13,14,15);
print_r($_SESSION);

---
"One who mix sports and patriotism is a barbarian"
Email: rrjanbiah-at-Y!com
Jul 17 '05 #2

P: n/a
Thanks Rajesh, but not quite what I wanted. Let me perhaps explain
what I need more fully:

Lets say I have a multidimensional array inside my PHP page, eg:

$myMDArray[$x]
$myMDArray[$x][$y]
$myMDArray[$x][$y][$z]

where $x, $y and $z may be many different values, and, as shown, there
may be data at various levels in the dimensional hierarchy.

How can I dump this complete array into $_SESSION, and then do the
reverse to recover it again in a later page?

Specifically, do I have to do it laboriously with "foreach" loops
through each level in $myMDArray, or can I use some simple merge
function or operator?

Rob
On 20 Nov 2003 06:31:36 -0800, ng**********@rediffmail.com (R. Rajesh
Jeba Anbiah) wrote:
Rob Tweed <rt****@blueyonder.co.uk> wrote in message news:<k2********************************@4ax.com>. ..
Probably a simple question but I can't find the answer anyway.

Specifically, is it possible to copy a multidimensional array into the
$_SESSION array - ie a deep clone of all keys and data?
Global DOMination with eXtc : http://www.mgateway.tzo.com


Example:
$_SESSION['string1'] = 'String1';
$_SESSION['string2'] = 'String2';
$_SESSION['array1'] = array(1,2,3,4,5);
$_SESSION['array2'] = array(11,12,13,14,15);
print_r($_SESSION);

---
"One who mix sports and patriotism is a barbarian"
Email: rrjanbiah-at-Y!com


---
Rob Tweed
M/Gateway Developments Ltd

Global DOMination with eXtc : http://www.mgateway.tzo.com
---
Jul 17 '05 #3

P: n/a

On 20-Nov-2003, Rob Tweed <rt****@blueyonder.co.uk> wrote:

On 20 Nov 2003 06:31:36 -0800, ng**********@rediffmail.com (R. Rajesh
Jeba Anbiah) wrote:
Rob Tweed <rt****@blueyonder.co.uk> wrote in message
news:<k2********************************@4ax.com> ...
Probably a simple question but I can't find the answer anyway.

Specifically, is it possible to copy a multidimensional array into the
$_SESSION array - ie a deep clone of all keys and data?
Global DOMination with eXtc : http://www.mgateway.tzo.com


Example:
$_SESSION['string1'] = 'String1';
$_SESSION['string2'] = 'String2';
$_SESSION['array1'] = array(1,2,3,4,5);
$_SESSION['array2'] = array(11,12,13,14,15);
print_r($_SESSION);

---

Lets say I have a multidimensional array inside my PHP page, eg:

$myMDArray[$x]
$myMDArray[$x][$y]
$myMDArray[$x][$y][$z]

where $x, $y and $z may be many different values, and, as shown, there
may be data at various levels in the dimensional hierarchy.

How can I dump this complete array into $_SESSION, and then do the
reverse to recover it again in a later page?

Specifically, do I have to do it laboriously with "foreach" loops
through each level in $myMDArray, or can I use some simple merge
function or operator?


I don't see why you can't just say $_SESSION['myMDArray'] = $myMDArray;
However, if it really doesn't work you can always serialize()/unserialize()
the array before/after putting it in the $_SESSION superglobal.

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #4

P: n/a
Rob Tweed <rt****@blueyonder.co.uk> schrieb:
Specifically, is it possible to copy a multidimensional array into the
$_SESSION array - ie a deep clone of all keys and data?
Yes.
I naively assumed that

$_SESSION["myArray"'] = $myArray ;

would work but it doesn't appear to work.


Your code is
$_SESSION['myArray'] = $myArray ;
I assume.

That should work and it works for me. There might be problems with this,
if register_globals is _on_. If register_globals is activated, then
change the name of the array index or the name of the variable:

$_SESSION['xmyArray'] = $myArray ;
or
$_SESSION['myArray'] = $xmyArray ;

Regards,
Matthias
Jul 17 '05 #5

P: n/a
The one thing I didn't see in your code sample was a call to
session_start() before any of the references to $_SESSION. This
should really be the first line in your application and might be your
problem.

Wes
> Specifically, is it possible to copy a multidimensional array into the
> $_SESSION array - ie a deep clone of all keys and data?
> Global DOMination with eXtc : http://www.mgateway.tzo.com

Example:
$_SESSION['string1'] = 'String1';
$_SESSION['string2'] = 'String2';
$_SESSION['array1'] = array(1,2,3,4,5);
$_SESSION['array2'] = array(11,12,13,14,15);
print_r($_SESSION);

---

Lets say I have a multidimensional array inside my PHP page, eg:

$myMDArray[$x]
$myMDArray[$x][$y]
$myMDArray[$x][$y][$z]

where $x, $y and $z may be many different values, and, as shown, there
may be data at various levels in the dimensional hierarchy.

How can I dump this complete array into $_SESSION, and then do the
reverse to recover it again in a later page?

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.