Passing arrays as url parameter

What is the best way to pass an array as a url parameter? I thought if this is possible:

$aValues = array(); $url = 'http://www.example.com?aParam='.$aValues; 

or how about it:

 $url = 'http://www.example.com?aParam[]='.$aValues; 

I read examples, but I find this messy:

 $url = 'http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3'; 
+80
arrays url php
Nov 19 '09 at 14:07
source share
8 answers

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 line is aParam[0]=1&aParam[1]=4&aParam[a]=b&aParam[c]=d .

+171
Nov 19 '09 at 15:42
source share

Edit: Don’t miss Stefan's solution above, which uses the very handy http_build_query() function: https://stackoverflow.com/a/164648/

knittl is right on the shoot. However, there is an easier way to do this:

 $url = 'http://example.com/index.php?'; $url .= 'aValues[]=' . implode('&aValues[]=', array_map('urlencode', $aValues)); 

If you want to do this with an associative array, try this instead:

PHP 5.3+ (lambda function)

 $url = 'http://example.com/index.php?'; $url .= implode('&', array_map(function($key, $val) { return 'aValues[' . urlencode($key) . ']=' . urlencode($val); }, array_keys($aValues), $aValues) ); 

PHP <5.3 (callback)

 function urlify($key, $val) { return 'aValues[' . urlencode($key) . ']=' . urlencode($val); } $url = 'http://example.com/index.php?'; $url .= implode('&amp;', array_map('urlify', array_keys($aValues), $aValues)); 
+52
Nov 19 '09 at 14:33
source share

The easiest way is to use the serialize function.

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

This variable can be restored using unserialize

So, going to the URL you are using:

$url = urlencode(serialize($array))

and to restore the variable used

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

Be careful. The maximum size of a GET request is limited to 4k, which you can easily overcome by passing arrays to URLs.

In addition, this is really not the safest way to transfer data! You should probably study using sessions.

+9
Nov 19 '09 at 14:23
source share

please avoid variables when outputting ( urlencode ).

and you cannot just print the array, you need to build your url using some kind of loop.

 $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); } 
+5
Nov 19 '09 at 14:09
source share

I am doing this with a serial database encoding64. The best and smallest way, I think. urlencode is a lot of space and you have only 4k.

+3
May 08 '14 at 14:07
source share
  <?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 via the url to page_no_2.php ,

ex: -

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

To return to the original array, it must be urldecode() and then unserialize() , as 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

+2
Nov 22 '13 at 11:42
source share

This is not a direct answer, as this has already been answered, but everyone was talking about data transfer, but no one said what you do when he gets there, and it took me half an hour to work outside. So I thought I would help here.

I will repeat this bit

 $data = array( 'cat' => 'moggy', 'dog' => 'mutt' ); $query = http_build_query(array('mydata' => $data)); $query=urlencode($query); 

Obviously you format it better than this www.someurl.com?x=$query

And to return the data

 parse_str($_GET['x']); echo $mydata['dog']; echo $mydata['cat']; 
0
Sep 07 '16 at 13:13
source share
 **in create url page** $data = array( 'car' => 'Suzuki', 'Model' => '1976' ); $query = http_build_query(array('myArray' => $data)); $url=urlencode($query); echo" <p><a href=\"index2.php?data=".$url."\"> Send </a><br /> </p>"; **in received page** parse_str($_GET['data']); echo $myArray['car']; echo '<br/>'; echo $myArray['model']; 
0
May 24 '17 at 19:18
source share



All Articles