是否有PHP函数将查询字符串转换为数组? [英] Is there a PHP function to convert a query string to an array?

查看:196
本文介绍了是否有PHP函数将查询字符串转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是在寻找http_build_query()的反面.

I'm basically looking for the opposite of http_build_query().

我将以下内容作为字符串:

I have the following as a string:

foo=bar&bar[var]=foo

我想要以下内容(传递给http_build_query):

And I want the following (to pass into http_build_query):

array(
    'foo' => 'bar',
    'bar' => array(
         'var' => 'foo',
    )
)

推荐答案

您要

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:

<?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

请注意,这是 http_build_query上列出的第一个相关功能. 页面.

Notice this is the very first related function listed on the http_build_query page.

这篇关于是否有PHP函数将查询字符串转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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