Coder Perfect

Arrays are passed as url parameters.


What is the most efficient method of passing an array as a url parameter? If it’s possible, I’d like to try:

$aValues = array();

$url = ''.$aValues;

or consider this:

$url = '[]='.$aValues;

I’ve seen few examples, but I think it’s a mess:

$url = '[]=value1&aParam[]=value2&aParam[]=value3';

Asked by uji

Solution #1

http build query is a pretty simple solution (). It uses an associative array to store your query parameters:

$data = array(
    '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"

aParam[0]=1&aParam[1]=4 to aParam[0]=1&aParam[1]=4 &aParam[a]=b&aParam[c]=d.

Answered by Stefan Gehrig

Solution #2

Edit: Don’t forget to check out Stefan’s approach, which makes use of the extremely useful http build query() function:

When it comes to fleeing, knittl is spot on. There is, however, a more straightforward method:

$url = '';
$url .= 'aValues[]=' . implode('&aValues[]=', array_map('urlencode', $aValues));

Try this instead if you want to do it with an associative array:

PHP 5.3 or above is required (lambda function)

$url = '';
$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 = '';
$url .= implode('&amp;', array_map('urlify', array_keys($aValues), $aValues));

Answered by Jordan Running

Solution #3

Easiest way would be to use the serialize function.

Any variable can be serialized for storage or transmission. It’s described in the php handbook – serialize.

Using unserialize, the variable can be restored.

As a result, in the URL passing, you use:

$url = urlencode(serialize($array))

and to re-establish the variable

$var = unserialize(urldecode($_GET[‘array’]))

But proceed with caution. A GET request’s maximum size is limited to 4k, which you can easily exceed by include arrays in the URL.

Furthermore, it is not the safest method of data transmission! Instead, you should probably consider using sessions.

Answered by nash

Solution #4

When printing, please make sure to escape your variables (urlencode).

You can’t just output an array; you’ll need to use a loop to generate your url.

$url = ''
$first = true;
foreach($aValues as $key => $value) {
  if(!$first) $url .= '&amp';
  else $first = false;
  $url .= 'aValues['.urlencode($key).']='.urlencode($value);

Answered by knittl

Solution #5

$array["a"] = "Thusitha";
$array["b"] = "Sumanadasa";
$array["c"] = "Lakmal";
$array["d"] = "Nanayakkara";

$str = serialize($array);
$strenc = urlencode($str);
print $str . "\n";
print $strenc . "\n";

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”; results in


So, if you wish to give this $array to page no 2.php through URL,


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

To go back to the original array, use urldecode() followed by unserialize(), as shown in page no 2.php:

    $strenc2= $_GET['data'];
    $arr = unserialize(urldecode($strenc2));


 array(4) {
  string(8) "Thusitha"
  string(10) "Sumanadasa"
  string(6) "Lakmal"
  string(11) "Nanayakkara"

again 😀

Answered by Thusitha Sumanadasa

Post is based on