Coder Perfect

Arrays are passed as url parameters.

Problem

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 = 'http://www.example.com?aParam='.$aValues;

or consider this:

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

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

$url = 'http://www.example.com?aParam[]=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(
    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"

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: https://stackoverflow.com/a/1764199/179125

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

$url = 'http://example.com/index.php?';
$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 = '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));

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 = '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);
}

Answered by knittl

Solution #5

 <?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";
?> 

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

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 wish to give this $array to page no 2.php through URL,

ex:-

$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:

    <?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 😀

Answered by Thusitha Sumanadasa

Post is based on https://stackoverflow.com/questions/1763508/passing-arrays-as-url-parameter