具有相同名称参数的 http_build_query [英] http_build_query with same name parameters
本文介绍了具有相同名称参数的 http_build_query的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法使用同名参数通过 http_build_query
自动构建查询?
Is there a way to build a query automatically with http_build_query
using parameters with the same name?
如果我做类似的事情
array('foo' => 'x', 'foo' => 'y');
它们显然在数组中被覆盖,但即使我这样做:
They are obviously overwritten within the array, but even if I do:
array('foo' => array('x', 'y'));
该函数创建了类似 foo[0]=x&foo[1]
的东西,这不是我想要的,因为我需要这种格式的参数 foo=x&foo=y
.
The function creates something like foo[0]=x&foo[1]
, which isn't what I want, since I need the parameters in this format foo=x&foo=y
.
推荐答案
这应该可以满足您的需求,我有一个需要相同功能的 API.
This should do what you want, I had an api that required the same thing.
$vars = array('foo' => array('x','y'));
$query = http_build_query($vars, null, '&');
$string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query); //foo=x&foo=y
这篇关于具有相同名称参数的 http_build_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文