我基本上是在寻找与 http_build_query()
相反的东西。
我有以下字符串:
foo=bar&bar[var]=foo
我想要以下内容(传递到 http_build_query
):
array(
'foo' => 'bar',
'bar' => array(
'var' => 'foo',
)
)
最佳答案
你想要parse_str()
.将数组作为第二个参数传递给它,它会从你给它的查询字符串中提取变量到数组中:
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);
print_r($output);
/*
Array
(
[first] => value
[arr] => Array
(
[0] => foo bar
[1] => baz
)
)
*/
请注意,这是 http_build_query
中列出的第一个相关函数页面。
关于php - 是否有将查询字符串转换为数组的 PHP 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3951454/