copying a multidimensional array to $_SESSION

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default copying a multidimensional array to $_SESSION

    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 : [url]http://www.mgateway.tzo.com[/url]
    ---
    Rob Tweed Guest

  2. Similar Questions and Discussions

    1. multidimensional array assignment
      dear all, I am a facing a problem in multidimensional array. The problem is that when i am trying to copy the value of arry named 'str' to...
    2. creating a multidimensional array
      How can I combine two arrays in cfscript to form one multidimensional array? For example, I have two arrays: firstarray=fred; firstarray=jim;...
    3. Multidimensional array: see if 1st key is available
      Hi, I've got an multidimensional array $ret = $country_code; Now I want to see if $countryCode is even in that array, because if it's not, it...
    4. copying a multidimensional array
      Hi: whatz the best way to copy an multidimensional array onto another. I have never used something like clone, just want to know whatz the easiest...
    5. Split multidimensional array into 4 multidimensional arrays
      Hello everyone, I have a multidimensional array that I need to split into 4 multidimensional arrays. I've tried the examples from the...
  3. #2

    Default Re: copying a multidimensional array to $_SESSION

    Rob Tweed <rtweed@blueyonder.co.uk> wrote in message news:<k2aprv8jmmggldr6ofu1qpid77v1cp81lj@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 : [url]http://www.mgateway.tzo.com[/url]
    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
    R. Rajesh Jeba Anbiah Guest

  4. #3

    Default Re: copying a multidimensional array to $_SESSION

    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, [email]ng4rrjanbiah@rediffmail.com[/email] (R. Rajesh
    Jeba Anbiah) wrote:
    >Rob Tweed <rtweed@blueyonder.co.uk> wrote in message news:<k2aprv8jmmggldr6ofu1qpid77v1cp81lj@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 : [url]http://www.mgateway.tzo.com[/url]
    >
    > 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 : [url]http://www.mgateway.tzo.com[/url]
    ---
    Rob Tweed Guest

  5. #4

    Default Re: copying a multidimensional array to $_SESSION


    On 20-Nov-2003, Rob Tweed <rtweed@blueyonder.co.uk> wrote:
    >
    > On 20 Nov 2003 06:31:36 -0800, [email]ng4rrjanbiah@rediffmail.com[/email] (R. Rajesh
    > Jeba Anbiah) wrote:
    >
    > >Rob Tweed <rtweed@blueyonder.co.uk> wrote in message
    > >news:<k2aprv8jmmggldr6ofu1qpid77v1cp81lj@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 : [url]http://www.mgateway.tzo.com[/url]
    > >
    > > 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
    [url]www.creative-light.com[/url]
    tom (at) creative (dash) light (dot) com
    do NOT send email to [email]jamesbutler@willglen.net[/email] (it's reserved for spammers)
    Tom Thackrey Guest

  6. #5

    Default Re: copying a multidimensional array to $_SESSION

    Rob Tweed <rtweed@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
    Matthias Esken Guest

  7. #6

    Default Re: copying a multidimensional array to $_SESSION

    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 : [url]http://www.mgateway.tzo.com[/url]
    > > >
    > > > 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?
    > >
    Wes Bailey Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139