Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have multiple $_GET with the same key, different values?

Tags:

php

get

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!

like image 547
d-_-b Avatar asked May 21 '12 02:05

d-_-b


3 Answers

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

like image 119
Vjy Avatar answered Sep 17 '22 07:09

Vjy


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.

like image 37
K-ballo Avatar answered Sep 21 '22 07:09

K-ballo


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)
like image 45
KahWee Teng Avatar answered Sep 18 '22 07:09

KahWee Teng