tags:

views:

441

answers:

4

hi,

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?aParam='.$aValues;

or how about this:

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

Ive read examples, but i find it messy:

$url = 'http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3';

Thanks in advance.

+3  A: 

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);
}
knittl
+1 for escaping
Nicky De Maeyer
nash
+1  A: 

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.

nash
serializing is also a nice way of doing it, but then it's in a not-so-readable form anymore
knittl
Yeah, serialize isn't really a clean way of doing things when you're working with urls, because it expands the data so much. Better to go custom.
Tchalvak
the max size of the GET parameter was what i was worried about thats why i was (stupidly) hoping that the parser wont mind if its an array that is passed. i realized just now that it wont work without touching the max size. Thanks anyway nash, I think i will be doing it with sessions
uji
+6  A: 

knittl is right on about escaping. However, there's a simpler 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));
Jordan
neat! would be nice if it could work with associative arrays too. +1 anyway
knittl
knittl: We can do that with a callback function to array_map and passing the keys and values separately. Check it out.
Jordan
i wish there was a +2 ;) … me likey
knittl
Pretty slick. Functional programming FTW.
Tchalvak
but this could still exceed the max size of the GET parameter right? what are the odds if i use sessions instead just like nash mentioned below?
uji
Well, you can store a more or less unlimited amount of data in a session. The risk with sessions is in URLs breaking when the session expires, or if the user tries to do things e.g. in two tabs at once. If the user bookmarks the page, then comes back after their session expires, will they still get the page they expect? It sounds like you may need to think harder about your architecture and why you're passing around such huge arrays in the first place.
Jordan
+3  A: 

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.

Stefan Gehrig