← back to code snippets


PHP : GET multiple parameters with the same name from the URL

Posted on November 13th, 2013 in PHP by Graeme

When using PHP GET to retrieve values from the url, you may come accross a problem where you have multiple parameters with the same name in your URL, and you can only access one using GET.

One way to access multiple parameters with the same name is simply to get the entire query string using $_SERVER[‘QUERY_STRING’], and then exploding the query string to get what you need, just like this this (found on stackoverflow):

$query  = explode('&', $_SERVER['QUERY_STRING']);
$params = array();

foreach( $query as $param )
{

list($name, $value)= explode('=', $param);
  $params[urldecode($name)][] = urldecode($value);
}

Now when you do print_r($params), you’ll see that you’ll have an array for each parameter, containing the multiple values, rather than just one.