Passing an array by get method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Beginner1
    New Member
    • Feb 2007
    • 16

    Passing an array by get method

    Hi,
    I need to pass an array by link:

    <a href=somfile.ph p?array=$my_arr ay>

    I've tried to make it string (with join) but it doesn't work for big arrays (max size for string parameters is ~255 characters).

    Is there a way to pass a big array and get it in $GET variable?

    Thanks
  • Beginner1
    New Member
    • Feb 2007
    • 16

    #2
    I could pass it by using the serialize() function.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      You could put the array in a hidden variable and use javascript in an onlclick for the link to submit the form. You could store your array in a session variable. I don't think you can send larger bits of data over get, however.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        The total maximum size of a GET url string is approx. 2800 bytes (under Windows).

        That may look a lot, but it is not if you pass a page of text. So, when you suspect it to be larger, it is adviseable to POST the array. You can use the PHP cURL functions to do that for you.

        Ronald :cool:

        Comment

        • Beginner1
          New Member
          • Feb 2007
          • 16

          #5
          Thank you for your replies!

          When I was writhing my question I knew nothing about this target. Now with your help I know too much and can't make the right decision:)

          Can you help me to choose the most suitable and correct way in this case:

          I have an array with integer values, its length could be about 2000 elements. Actually I need it in the next page as a string (joined with comma).

          My first question is pass it as string or an array? and second how?

          I've tried $_SESSION and $_GET both work OK (some problems with $_GET in IE: the string was cut. but in Mozilla works OK ). Also I've checked my PHP installed with cURL, so I can use it. Or maybe Javascript???

          Thanks in Advance

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            What is wrong with using the $_SESSION array for this. You can assign your array to $_SESSION and use this in another page. As long as you do not forget to use start_session at the top of the scripts!

            The following sample shows this. It sets up an aray of 1000 entries, each entry also holding an array. It is then assigned to the $_SESSION array and the content is printed out. Try it out.

            [php]<?php
            session_start() ;

            $myArray = array(); // define the test array
            $max = 1000; // no of array entries to build

            // construct the test array
            for ($i=0; $i<$max; $i++) {
            $myArray[$i] = array("test$i" => "value$i");
            }

            // show the test array
            echo '<b>Content Dump Of The Array I Built:</b><br>';
            echo '<pre>'; print_r($myArra y);

            // assign the test array to the $_SESSION array
            $_SESSION['anarray'] = $myArray;

            // show the test array in $_SESSION
            echo '<b>Content Dump Of $_SESSION Array:</b><br>';
            print_r($_SESSI ON);

            // show each entry of the test array in the $_SESSION array
            echo '<b>Content Of My Array In $_SESSION Array:</b><br>';
            for ($i=0; $i<max; $i++) {
            echo "key=test$i - value={$_SESSIO N['anarray'][$i]["test$i"]}<br>";
            }
            ?>[/php]
            Ronald :cool:

            Comment

            • Beginner1
              New Member
              • Feb 2007
              • 16

              #7
              Thank you very much!!!!

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                You are most welcome.

                Ronald :cool:

                Comment

                Working...