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

风流意气都作罢 提交于 2019-11-27 04:46:16

问题


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!


回答1:


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




回答2:


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.




回答3:


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)



回答4:


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...



来源:https://stackoverflow.com/questions/10678918/can-i-have-multiple-get-with-the-same-key-different-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!