How to get PHP $_GET array?

0 votes
asked Dec 2, 2009 by faya

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?

10 Answers

0 votes
answered Dec 2, 2009 by seengee

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 = "".$foo;

and on page.php

$vars = unserialize($_GET['vars']);
0 votes
answered Dec 2, 2009 by sampson

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


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:


But that clearly would be much more verbose.

0 votes
answered Dec 2, 2009 by jordan-running

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


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

0 votes
answered Dec 2, 2009 by christophed

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]);
0 votes
answered Dec 2, 2009 by lucas-oman

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:

  '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[].

0 votes
answered Dec 2, 2009 by gumbo
0 votes
answered Dec 2, 2009 by chronofish

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


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";
0 votes
answered Dec 30, 2012 by michael-j-calkins

Put all the ids into a variable called $ids and separate them with a ",":

$ids = "1,2,3,4,5,6";

Pass them like so:

$url = "?ids={$ids}";

Process them:

$ids = explode(",", $_GET['ids']);
0 votes
answered Dec 29, 2013 by raju

Yes, here's a code example with some explanation in comments:

 // Fill up array with names

$sql=mysql_query("SELECT * FROM fb_registration");


//get the q parameter from URL

//lookup all hints from array if length of q>0

if (strlen($q) > 0)
  for($i=0; $i<count($a); $i++)
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      if ($hint=="")
        $hint=$hint." , ".$a[$i];
0 votes
answered Dec 5, 2014 by pmesco
$url = 'http://link/foo.php?id=1-2-3';
$id = explode('-', $url);
echo $id[0];
echo $id[1];
echo $id[2];
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter