是否有将查询字符串转换为数组的 PHP 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3951454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a PHP function to convert a query string to an array?
提问by Darryl Hein
I'm basically looking for the opposite of http_build_query()
.
我基本上是在寻找http_build_query()
.
I have the following as a string:
我有以下字符串:
foo=bar&bar[var]=foo
And I want the following (to pass into http_build_query
):
我想要以下内容(传递到http_build_query
):
array(
'foo' => 'bar',
'bar' => array(
'var' => 'foo',
)
)
回答by meagar
You want parse_str()
. Pass it an array as the 2nd parameter and it will extract variables from the query string you give it into the array:
你要parse_str()
。将数组作为第二个参数传递给它,它将从您提供给数组的查询字符串中提取变量:
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
Notice this is the very first related function listed on the http_build_query
page.
请注意,这是http_build_query
页面上列出的第一个相关功能。