php url查询嵌套数组没有索引 [英] php url query nested array with no index
问题描述
我正在使用第三方API,它接收几个必须编码的参数,如下所示:
text [] =你好%20World&安培;文本[] =%如何%20顷&20you放大器; HTML [] =< p为H.仅有%20fine,%20thank%20you< / p为H.
正如你所看到的,这个API可以接受多个文本参数,也可以接受HTML(不在示例调用)。
我已经使用http_build_query为其他API正确构建了一个查询字符串
$ params ['text'] [] ='Hello World';
$ params ['text'] [] ='你好吗?';
$ params ['html'] [] ='< p>很好,谢谢< / p>';
$ http_query = http_build_query($ params);
这种方法的问题在于它会用数字索引构建一个查询字符串:
text [0] = Hello%20World& text [1] =%20如何%20you?& html [0] =< p> Just %20fine,%20thank%20you< / p>
不幸的是,我正在使用的API不喜欢数字索引, 。
是否有任何php函数/类方法可以帮助我快速构建查询?
谢谢
似乎没有办法通过 我没有测试过它。如果它不起作用,那么你将不得不推出自己的。也许你可以发布github的要点,以便其他人可以使用它! I'm working with a third party API that receives several parameters which must be encoded like this: As you can see this API can accept multiple parameters for text, and also for HTML (not in the sample call). I have used http_build_query to correctly build a query string for other APIs The problem with this approach is that it will build a query string with the numeric index: unfortunately the API I'm working with doesn't like the numeric index and fails. Is there any php function/class-method that can help me build a query like this quickly? Thank you There doesn't seem to be a way to do this with I haven't tested it. If it doesn't work then you're going to have to roll your own. Maybe you can publish a github gist so other people can use it! 这篇关于php url查询嵌套数组没有索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! http_build_query
。抱歉。在文档页面上,有人有这样的:
$ p $ function cr_post($ a,$ b = 0,$ c = 0) {
if(!is_array($ a))return false;
foreach((array)$ a as $ k => $ v){
if($ c)$ k = $ b。[]; elseif(is_int($ k))$ k = $ b。$ k;
if(is_array($ v)|| is_object($ v)){
$ r [] = cr_post($ v,$ k,1); continue;
}
$ r [] = urlencode($ k)。=.urlencode($ v);
}
return implode(&,$ r);
}
$ params ['text'] [] ='Hello World';
$ params ['text'] [] ='你好吗?';
$ params ['html'] [] ='< p>很好,谢谢< / p>';
$ str = cr_post($ params);
echo $ str;
text[]=Hello%20World&text[]=How%20are%20you?&html[]=<p>Just%20fine,%20thank%20you</p>
$params['text'][] = 'Hello World';
$params['text'][] = 'How are you?';
$params['html'][] = '<p>Just fine, thank you</p>';
$http_query = http_build_query($params);
text[0]=Hello%20World&text[1]=How%20are%20you?&html[0]=<p>Just%20fine,%20thank%20you</p>
http_build_query
. Sorry. On the docs page though, someone has this:function cr_post($a,$b=0,$c=0){
if (!is_array($a)) return false;
foreach ((array)$a as $k=>$v){
if ($c) $k=$b."[]"; elseif (is_int($k)) $k=$b.$k;
if (is_array($v)||is_object($v)) {
$r[]=cr_post($v,$k,1);continue;
}
$r[]=urlencode($k)."=" .urlencode($v);
}
return implode("&",$r);
}
$params['text'][] = 'Hello World';
$params['text'][] = 'How are you?';
$params['html'][] = '<p>Just fine, thank you</p>';
$str = cr_post($params);
echo $str;