且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将URL参数列表字符串分解为成对的[key] => [值]数组?

更新时间:2023-11-18 18:44:40

使用PHP的 parse_str 函数.

Use PHP's parse_str function.

$str = 'a=1&b=2&c=3';
$exploded = array();
parse_str($str, $exploded);
$exploded['a']; // 1

我想知道您从何处获得此字符串?如果它是问号后面的URL的一部分(URL的查询字符串),则您已经可以通过超全局$_GET数组来访问它:

I wonder where you get this string from? If it's part of the URL after the question mark (the query string of an URL), you can already access it via the superglobal $_GET array:

# in script requested with http://example.com/script.php?a=1&b=2&c=3
$_GET['a']; // 1
var_dump($_GET); // array(3) { ['a'] => string(1) '1', ['b'] => string(1) '2', ['c'] => string(1) '3' )