Home >Backend Development >PHP Tutorial >How Can I Retrieve Multiple Parameters with the Same Name from a URL in PHP?
Retrieving Multiple Parameters with the Same Name from a URL in PHP
When handling URLs containing multiple parameters with the same name, PHP's default behavior using $_GET only returns the last value assigned. To access all values for a given key, consider the following approach:
$query = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
foreach ($query as $param) { // Handle null value with '=' if (strpos($param, '=') === false) $param += '='; list($name, $value) = explode('=', $param, 2); }
$params[urldecode($name)][] = urldecode($value); }
Using this approach, you can access the multiple parameter values as follows:
print_r($params['rft_id']); // Array ('info:oclcnum/1903126', 'http://www.biodiversitylibrary.org/bibliography/4323')
The above is the detailed content of How Can I Retrieve Multiple Parameters with the Same Name from a URL in PHP?. For more information, please follow other related articles on the PHP Chinese website!