php - 是否有将查询字符串转换为数组的 PHP 函数?

标签 php

我基本上是在寻找与 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/

相关文章:

php - .htaccess url 重写后,某些url 重写的页面无法执行注销

php - Slim 3路线参数

php - 从具有多行的关系表中获取数据

php - 只是 PHP 无法在 Win7 64 位上安装 Wamp-Server

php - Zend_db 仅在我的主模块中出现错误,并且仅当我使用 'woningen' 作为表名时

PHP Mysql脚本错误

javascript - AJAX 不工作,通过 JS 更新数据库

php - PHP 中的基本 URL 重写问题 (mod_rewrite)

php - Mysql查询返回表结构

PHP session 在本地主机上工作,但在实际服务器上不工作