Home  >  Article  >  Backend Development  >  PHP中,把$_SERVER['QUERY_STRING']转换为$_GET数组

PHP中,把$_SERVER['QUERY_STRING']转换为$_GET数组

WBOY
WBOYOriginal
2016-06-06 20:51:121616browse

$_SERVER['QUERY_STRING']的字符串类似:

a=1&b=2&c=3&vardate=2012-11-12

希望能转换成$_GET数组,内容类似于:

$_GET['a']=1;
$_GET['b']=2;
$_GET['c']=3;
$_GET['vardate']='2012-11-12';

我的思路是把原始字符串根据&切分后,再根据=切分。

list(,$tmp) = explode("?", $_SERVER['QUERY_STRING']);
$a = explode("&",$tmp);
foreach($a as $v) {
    $b = explode("=", $v);
	$_GET[$b[0]] = $b[1];			
}
var_dump($_GET);

不考虑数值类型。
求一个比较优雅的代码。

回复内容:

$_SERVER['QUERY_STRING']的字符串类似:

a=1&b=2&c=3&vardate=2012-11-12

希望能转换成$_GET数组,内容类似于:

$_GET['a']=1;
$_GET['b']=2;
$_GET['c']=3;
$_GET['vardate']='2012-11-12';

我的思路是把原始字符串根据&切分后,再根据=切分。

list(,$tmp) = explode("?", $_SERVER['QUERY_STRING']);
$a = explode("&",$tmp);
foreach($a as $v) {
    $b = explode("=", $v);
	$_GET[$b[0]] = $b[1];			
}
var_dump($_GET);

不考虑数值类型。
求一个比较优雅的代码。

请使用parse_str函数

parse_str($_SERVER['QUERY_STRING'], $get);
print_r($get);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn