views:

1351

answers:

7

Hello,

I have a question. Is it possible to have a value in $_GET as an array?

If I am trying to send a link with http://link/foo.php?id=1&id=2&id=3, and I want to use $_GET['id'] on the php side, how can that value be an array? Because right now echo $_GET['id'] is returning 3. Its the last id which is in the header link. Any suggestions?

+1  A: 

I think i know what you mean, if you want to send an array through a URL you can use serialize

for example:

$foo = array(1,2,3);
$serialized_array = serialize($foo);
$url = "http://www.foo.com/page.php?vars=".$foo;

and on page.php

$vars = unserialize($_GET['vars']);
seengee
+2  A: 

You could make id a series of comma-seperated values, like this:

index.php?id=1,2,3&name=john

Then, within your PHP code, explode it into an array:

$values = explode(",", $_GET["id"]);
print count($values) . " values passed.";

This will maintain brevity. The other (more commonly used with $_POST) method is to use array-style square-brackets:

index.php?id[]=1&id[]=2&id[]=3&name=john

But that clearly would be much more verbose.

Jonathan Sampson
+15  A: 

The usual way to do this in PHP is to put id[] in your URL instead of just id:

http://link/foo.php?id[]=1&id[]=2&id[]=3

Then $_GET['id'] will be an array of those values. It's not especially pretty, but it works out of the box.

Jordan
This is typically the way I do it, but you can process the request wihtout changing the form (see my response below using QueryString)
ChronoFish
+3  A: 

You can specify an array in your HTML this way:

<input type="hidden" name="id[]" value="1"/>
<input type="hidden" name="id[]" value="2"/>
<input type="hidden" name="id[]" value="3"/>

This will result in this $_GET array in PHP:

array(
  'id' => array(
    0 => 1,
    1 => 2,
    2 => 3
  )
)

Of course, you can use any sort of HTML input, here. The important thing is that all inputs whose values you want in the 'id' array have the name id[].

Lucas Oman
+3  A: 

When you don't want to change the link (e.g. foo.php?id=1&id=2&id=3) you could probably do something like this (although there might be a better way...):

$id_arr = array();
foreach (explode("&", $_SERVER['QUERY_STRING']) as $tmp_arr_param) {
    $split_param = explode("=", $tmp_arr_param);
    if ($split_param[0] == "id") {
        $id_arr[] = urldecode($split_param[1]);
    }
}
print_r($id_arr);
ChristopheD
+1, This definitely is in need of more error checking etc. You should be using `$_SERVER['QUERY_STRING']` there, and `urldecode()` the parameter in case there are funny characters in it... But I hate PHP's `[]` with a passion, so I have in fact written a function like this once.
mercator
Good points, thanks
ChristopheD
+3  A: 

You can get them using the Query String:

$idArray = explode('&',$_SERVER["QUERY_STRING"]);

This will give you:

$idArray[0] = "id=1";
$idArray[1] = "id=2";
$idArray[2] = "id=3";

Then

foreach ($idArray as $index => $avPair)
{
  list($ignore, $value) = explode("=", $avPair);
  $id[$index] = $value;
}

This will give you

$id[0] = "1";
$id[1] = "2";
$id[2] = "3";
ChronoFish