如何让PHP将多个参数实例识别为数组?

Is there an option in PHP via (php.ini) probably to recognize a parameter passed multiple times in the URL as an array?

/cars.php?color=A&color=B

The above query should result in an array of colors ['A','B'] instead of the second parameter 'B' overwriting the first parameter 'A'

Use this:

/cars.php?color[]=A&color[]=B
             //^^        ^^

No need to enable anything in php.ini, accessing $_GET['color'] will return an array.

You can get the get params as string using:

$_SERVER['QUERY_STRING'];

So you could do something like this:

foreach(explode("&", $_SERVER['QUERY_STRING']) as $params) {
        list($key,$val) = explode("=",$params);
        $getArray[$key][] = $val;
}

var_dump($getArray);

However, this is really ugly, and other alternatives should be used (e.g., comma separated values)

If you dont want chage your url, you can use this :

$url = 'http://www.test.com/cars.php?color=A&color=B';

$parse_url = parse_url($url);

$array_color = array();
if($parse_url['query']){


    foreach (explode('&',$parse_url['query']) as $parameter)
    {
        $explode = explode('=',$parameter);

        if($explode[0]=='color'){
            $array_color[] = $explode[1];
        }
    }
}

var_dump($array_color);

I dont recommend it but it's work.