It bears mentioning that the parse_str builtin does NOT process a query string in the CGI standard way, when it comes to duplicate fields. If multiple fields of the same name exist in a query string, every other web processing language would read them into an array, but PHP silently overwrites them:
<?php
parse_str('foo=1&foo=2&foo=3');
$foo = array('foo' => '3');
?>
Instead, PHP uses a non-standards compliant practice of including brackets in fieldnames to achieve the same effect.
<?php
parse_str('foo[]=1&foo[]=2&foo[]=3');
$foo = array('foo' => array('1', '2', '3') );
?>
This can be confusing for anyone who's used to the CGI standard, so keep it in mind. As an alternative, I use a "proper" querystring parser function:
<?php
function proper_parse_str($str) {
$arr = array();
$pairs = explode('&', $str);
foreach ($pairs as $i) {
list($name,$value) = explode('=', $i, 2);
if( isset($arr[$name]) ) {
if( is_array($arr[$name]) ) {
$arr[$name][] = $value;
}
else {
$arr[$name] = array($arr[$name], $value);
}
}
else {
$arr[$name] = $value;
}
}
return $arr;
}
$query = proper_parse_str($_SERVER['QUERY_STRING']);
?>