copying a multidimensional array to $_SESSION

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Tweed

    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 multidimensiona l 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
    ---
  • R. Rajesh Jeba Anbiah

    #2
    Re: copying a multidimensiona l array to $_SESSION

    Rob Tweed <rtweed@blueyon der.co.uk> wrote in message news:<k2aprv8jm mggldr6ofu1qpid 77v1cp81lj@4ax. com>...[color=blue]
    > Probably a simple question but I can't find the answer anyway.
    >
    > Specifically, is it possible to copy a multidimensiona l array into the
    > $_SESSION array - ie a deep clone of all keys and data?
    > Global DOMination with eXtc : http://www.mgateway.tzo.com[/color]

    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($_SESSI ON);

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

    Comment

    • Rob Tweed

      #3
      Re: copying a multidimensiona l 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 multidimensiona l 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, ng4rrjanbiah@re diffmail.com (R. Rajesh
      Jeba Anbiah) wrote:
      [color=blue]
      >Rob Tweed <rtweed@blueyon der.co.uk> wrote in message news:<k2aprv8jm mggldr6ofu1qpid 77v1cp81lj@4ax. com>...[color=green]
      >> Probably a simple question but I can't find the answer anyway.
      >>
      >> Specifically, is it possible to copy a multidimensiona l array into the
      >> $_SESSION array - ie a deep clone of all keys and data?
      >> Global DOMination with eXtc : http://www.mgateway.tzo.com[/color]
      >
      > 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($_SESS ION);
      >
      >---
      > "One who mix sports and patriotism is a barbarian"
      >Email: rrjanbiah-at-Y!com[/color]

      ---
      Rob Tweed
      M/Gateway Developments Ltd

      Global DOMination with eXtc : http://www.mgateway.tzo.com
      ---

      Comment

      • Tom Thackrey

        #4
        Re: copying a multidimensiona l array to $_SESSION


        On 20-Nov-2003, Rob Tweed <rtweed@blueyon der.co.uk> wrote:
        [color=blue]
        >
        > On 20 Nov 2003 06:31:36 -0800, ng4rrjanbiah@re diffmail.com (R. Rajesh
        > Jeba Anbiah) wrote:
        >[color=green]
        > >Rob Tweed <rtweed@blueyon der.co.uk> wrote in message
        > >news:<k2aprv8j mmggldr6ofu1qpi d77v1cp81lj@4ax .com>...[color=darkred]
        > >> Probably a simple question but I can't find the answer anyway.
        > >>
        > >> Specifically, is it possible to copy a multidimensiona l array into the
        > >> $_SESSION array - ie a deep clone of all keys and data?
        > >> Global DOMination with eXtc : http://www.mgateway.tzo.com[/color]
        > >
        > > 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($_SESS ION);
        > >
        > >---[/color]
        > Lets say I have a multidimensiona l 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?
        >[/color]

        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

        tom (at) creative (dash) light (dot) com
        do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

        Comment

        • Matthias Esken

          #5
          Re: copying a multidimensiona l array to $_SESSION

          Rob Tweed <rtweed@blueyon der.co.uk> schrieb:
          [color=blue]
          > Specifically, is it possible to copy a multidimensiona l array into the
          > $_SESSION array - ie a deep clone of all keys and data?[/color]

          Yes.
          [color=blue]
          > I naively assumed that
          >
          > $_SESSION["myArray"'] = $myArray ;
          >
          > would work but it doesn't appear to work.[/color]

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

          That should work and it works for me. There might be problems with this,
          if register_global s is _on_. If register_global s 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

          Comment

          • Wes Bailey

            #6
            Re: copying a multidimensiona l 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
            [color=blue][color=green][color=darkred]
            > > >> Specifically, is it possible to copy a multidimensiona l 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($_SESS ION);
            > > >
            > > >---[/color]
            > > Lets say I have a multidimensiona l 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?
            > >[/color][/color]

            Comment

            Working...