如何将URL参数列表字符串分解为成对的[key] => [值]数组? [英] How to explode URL parameter list string into paired [key] => [value] Array?

查看:195
本文介绍了如何将URL参数列表字符串分解为成对的[key] => [值]数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
将查询字符串解析为数组

Possible Duplicate:
Parse query string into an array

我如何爆炸一个字符串,例如:

How can I explode a string such as:

a=1&b=2&c=3

它变成:

Array {
 [a] => 1
 [b] => 2
 [c] => 3
}

使用在&上定界的常规explode()函数将分隔参数,但不会成对出现[key] => [value].

Using the regular explode() function delimited on the & will separate the parameters but not in [key] => [value] pairs.

谢谢.

推荐答案

使用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' )

这篇关于如何将URL参数列表字符串分解为成对的[key] => [值]数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆