9

Is it possible to retrieve the arguments from a url where the same $_GET has different values?

Such as www.domain.com/?user=1&user=2

Currently this only shows whatever is listed second, so if I echo $_GET['user'], it would output 2

I couldn't seem to find this on SO, so if I missed it please let me know.

Thanks for your help!

d-_-b
  • 21,536
  • 40
  • 150
  • 256
  • 1
    See this [post](http://stackoverflow.com/questions/353379/how-to-get-multiple-parameters-with-same-name-from-a-url-in-php), `$_SERVER['QUERY_STRING']` is all you need. – deex May 21 '12 at 02:39

4 Answers4

10

Yes, use user[] as key. should work. PHP access all $_POST[] variables into an array?

Community
  • 1
  • 1
Vjy
  • 2,106
  • 3
  • 22
  • 34
  • 1
    Thanks! Just when I found the answer I check back and you got it too! I forgot it'd be the same as the checkbox/radios. So i'll be using ?user[]=1&user[]=2. Thank you! – d-_-b May 21 '12 at 02:41
5

Quick answer is no.

http://localhost/?user=1&user=2

Gets you:

array
    'user' => string '2' (length=1)

However, by including brackets in the query like this:

http://localhost/?user[]=1&user[]=2

You can retrieve $_GET['user'] and be returned with this:

array
    'user' => 
        array
            0 => string '1' (length=1)
            1 => string '2' (length=1)
KahWee Teng
  • 13,658
  • 3
  • 21
  • 21
4

The query string gets parsed into the associative array $_GET, so when there are duplicated keys only the last version of the value is present on the map. You can however access the raw $_SERVER['QUERY_STRING'] and parse it on your own.

If possible, it would we best if you modify your code to not duplicate keys.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
0

You could possibly use a foreach loop for each $_GET and then group all the 'user' variables into a single array and then access the whichever key value you need. 0 being the first, 1 being the second, and so on...

Tim Kipp
  • 200
  • 1
  • 16