how to pass an array in GET in PHP?

后端 未结 12 964
萌比男神i
萌比男神i 2020-11-28 14:37
$idArray = array(1,2,3,4);

can I write this line in HTML?

相关标签:
12条回答
  • 2020-11-28 14:57

    Use parse_str function

    $str = "first=value&arr[]=foo+bar&arr[]=baz"; 
    parse_str($str); 
    echo $first;  
    
    0 讨论(0)
  • 2020-11-28 15:00

    Use serialize and unserialize PHP function. This function giving you storable (string) version of array type. For more infomation about usage read http://php.net/manual/en/function.serialize.php and http://www.php.net/manual/en/function.unserialize.php

    0 讨论(0)
  • 2020-11-28 15:00

    serialize() your array first and pass that through. Then call unserialize() on it. http://ie.php.net/serialize

    0 讨论(0)
  • 2020-11-28 15:02

    You could use serialize and & serialize along side with urlencode e.g.

    On Sending you can send them like these:

    <?php
    $array1 = Array(["key1"]=>"value1",["key2"]=>"value2");
    $array2 = Array(["key1"]=>"value1",["key2"]=>"value2");
    $data1="textdata";
    
    $urlPortion= '&array1='.urlencode(serialize($array1)).
                 '&array2='.urlencode(serialize($array2)).
                 '&data1='.urlencode(serialize($data1));
    //Full URL:
    $fullUrl='http://localhost/?somevariable=somevalue'.$urlPortion
    ?>
    

    On Receiving you can access them as:

    <?php
    $destArray1=unserialize($_GET['array1']);
    $destArray2=unserialize($_GET['array2']);
    $destData1=unserialize($_GET['data1']);
    ?>
    

    And Voila, you can attach that url on ajax request or normal browser page.

    0 讨论(0)
  • 2020-11-28 15:05

    Another option (even nice looking, I'd say):

    <form method='POST'>
      <input type="hidden" name="idArray[]" value="1" />
      <input type="hidden" name="idArray[]" value="2" />
      <input type="hidden" name="idArray[]" value="3" />
      <input type="hidden" name="idArray[]" value="4" />
    </form>
    

    But of course it gets sent as POST. I wouldn'r recommend sending it with serialize since the output of that function can get pretty big and the length or URL is limited.

    with GET:

    <form method='GET'>
          <input type="hidden" name="idArray[]" value="1" />
          <input type="hidden" name="idArray[]" value="2" />
          <input type="hidden" name="idArray[]" value="3" />
          <input type="hidden" name="idArray[]" value="4" />
    </form>
    
    0 讨论(0)
  • 2020-11-28 15:08

    A session is a much safer and cleaner way to do this. Start your session with:

    session_start();
    

    Then add your serialized array as a session variable like this:

    $_SESSION["venue"] = serialize($venue);
    

    The simply call up the session variable when you need it.

    0 讨论(0)
提交回复
热议问题