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

WBOY
Release: 2016-06-06 20:51:12
Original
1615 people have browsed it

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

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

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

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

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

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

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

回复内容:

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

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

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

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

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

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

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

请使用parse_str函数

parse_str($_SERVER['QUERY_STRING'], $get);
print_r($get);
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template