Passing arrays as url parameter

前端 未结 10 1753
礼貌的吻别
礼貌的吻别 2020-11-22 12:32

What is the best way that I can pass an array as a url parameter? I was thinking if this is possible:

$aValues = array();

$url = \'http://www.example.com?a         


        
相关标签:
10条回答
  • 2020-11-22 12:50

    Easiest way would be to use the serialize function.

    It serializes any variable for storage or transfer. You can read about it in the php manual - serialize

    The variable can be restored by using unserialize

    So in the passing to the URL you use:

    $url = urlencode(serialize($array))

    and to restore the variable you use

    $var = unserialize(urldecode($_GET['array']))

    Be careful here though. The maximum size of a GET request is limited to 4k, which you can easily exceed by passing arrays in a URL.

    Also, its really not quite the safest way to pass data! You should probably look into using sessions instead.

    0 讨论(0)
  • 2020-11-22 13:00

    There is a very simple solution: http_build_query(). It takes your query parameters as an associative array:

    $data = array(
        1,
        4,
        'a' => 'b',
        'c' => 'd'
    );
    $query = http_build_query(array('aParam' => $data));
    

    will return

    string(63) "aParam%5B0%5D=1&aParam%5B1%5D=4&aParam%5Ba%5D=b&aParam%5Bc%5D=d"
    

    http_build_query() handles all the necessary escaping for you (%5B => [ and %5D => ]), so this string is equal to aParam[0]=1&aParam[1]=4&aParam[a]=b&aParam[c]=d.

    0 讨论(0)
  • 2020-11-22 13:00

    please escape your variables when outputting (urlencode).

    and you can’t just print an array, you have to build your url using a loop in some way

    $url = 'http://example.com/index.php?'
    $first = true;
    foreach($aValues as $key => $value) {
      if(!$first) $url .= '&amp';
      else $first = false;
      $url .= 'aValues['.urlencode($key).']='.urlencode($value);
    }
    
    0 讨论(0)
  • 2020-11-22 13:00
     <?php
    $array["a"] = "Thusitha";
    $array["b"] = "Sumanadasa";
    $array["c"] = "Lakmal";
    $array["d"] = "Nanayakkara";
    
    $str = serialize($array);
    $strenc = urlencode($str);
    print $str . "\n";
    print $strenc . "\n";
    ?> 
    

    print $str . "\n"; gives a:4:{s:1:"a";s:8:"Thusitha";s:1:"b";s:10:"Sumanadasa";s:1:"c";s:6:"Lakmal";s:1:"d";s:11:"Nanayakkara";} and

    print $strenc . "\n"; gives

    a%3A4%3A%7Bs%3A1%3A%22a%22%3Bs%3A8%3A%22Thusitha%22%3Bs%3A1%3A%22b%22%3Bs%3A10%3A%22Sumanadasa%22%3Bs%3A1%3A%22c%22%3Bs%3A6%3A%22Lakmal%22%3Bs%3A1%3A%22d%22%3Bs%3A11%3A%22Nanayakkara%22%3B%7D
    

    So if you want to pass this $array through URL to page_no_2.php,

    ex:-

    $url ='http://page_no_2.php?data=".$strenc."';
    

    To return back to the original array, it needs to be urldecode(), then unserialize(), like this in page_no_2.php:

        <?php
        $strenc2= $_GET['data'];
        $arr = unserialize(urldecode($strenc2));
        var_dump($arr);
        ?>
    

    gives

     array(4) {
      ["a"]=>
      string(8) "Thusitha"
      ["b"]=>
      string(10) "Sumanadasa"
      ["c"]=>
      string(6) "Lakmal"
      ["d"]=>
      string(11) "Nanayakkara"
    }
    

    again :D

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