Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无索引的php url查询嵌套数组_Php_Arrays_Function - Fatal编程技术网

无索引的php url查询嵌套数组

无索引的php url查询嵌套数组,php,arrays,function,Php,Arrays,Function,我正在使用第三方API,该API接收多个参数,这些参数必须按如下方式编码: text[]=Hello%20World&text[]=How%20are%20you?&html[]=<p>Just%20fine,%20thank%20you</p> preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($params)); text[]=你好%20World&text[]=你好吗%20%20yo

我正在使用第三方API,该API接收多个参数,这些参数必须按如下方式编码:

text[]=Hello%20World&text[]=How%20are%20you?&html[]=<p>Just%20fine,%20thank%20you</p>
preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($params));
text[]=你好%20World&text[]=你好吗%20%20you?&html[]=Just%20fine,%20thank%20you

正如您所见,此API可以接受文本和HTML的多个参数(不在示例调用中)

我已经使用http\u build\u query为其他api正确构建了查询字符串

$params['text'][] = 'Hello World';
$params['text'][] = 'How are you?';
$params['html'][] = '<p>Just fine, thank you</p>';

$http_query = http_build_query($params);
$params['text'][]='Hello World';
$params['text'][]=“你好吗?”;
$params['html'][]='很好,谢谢你

'; $http\u query=http\u build\u query($params);
此方法的问题在于,它将使用数字索引构建查询字符串:

text[0]=你好%20World&text[1]=你好吗%20%20you?&html[0]=刚刚好,%20thank%20you

不幸的是,我使用的API不喜欢数字索引,因此失败了

是否有任何php函数/类方法可以帮助我快速构建这样的查询


谢谢

使用
http\u build\u query
似乎没有办法做到这一点。很抱歉但在“文档”页面上,有人有以下信息:

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;
函数cr\u post($a、$b=0、$c=0){
如果(!is_数组($a))返回false;
foreach((数组)$a作为$k=>$v){
如果($c)$k=$b.“[]”elseif(is_int($k))$k=$b.$k;
if(is_数组($v)| is_对象($v)){
$r[]=cr_职位($v,$k,1);继续;
}
$r[]=urlencode($k)。“=”.urlencode($v);
}
返回内爆(&,$r);
}
$params['text'][]='Hello World';
$params['text'][]=“你好吗?”;
$params['html'][]='很好,谢谢你

'; $str=cr_post($params); echo$str;

我还没有测试过。如果它不起作用,那么你就得自己动手了。也许你可以发布一个github gist,让其他人可以使用它

我不知道标准的方法(我认为没有这种方法),但这里有一个丑陋的解决方案:

由于
[]
是由
http\u build\u query
编码的,所以您可以生成带有索引的字符串,然后替换它们

preg_replace('/(%5B)\d+(%5D=)/i', '$1$2', http_build_query($params));
试试这个:

$params['text'][] = 'Hello World';
$params['text'][] = 'How are you?';
$params['html'][] = '<p>Just fine, thank you</p>';
foreach ($params as $key => $value) {
    foreach ($value as $key2 => $value2) {        
        $http_query.= $key . "[]=" . $value2 . "&";
    }
}
$http_query = substr($http_query, 0, strlen($http_query)-1); // remove the last '&'
$http_query = str_replace(" ", "%20", $http_query); // manually encode spaces
echo $http_query;
$params['text'][]='Hello World';
$params['text'][]=“你好吗?”;
$params['html'][]='很好,谢谢你

'; foreach($key=>$value的参数){ foreach($key2=>$value2的值){ $http_query.=$key.[]=“$value2.”和“; } } $http\u query=substr($http\u query,0,strlen($http\u query)-1);//删除最后一个“&” $http_query=str_replace(“,“%20”,$http_query);//手动编码空间 echo$http\u查询;
我非常同意RiaD的回答,但您可能会在这段代码中遇到一些问题(很抱歉,由于缺少代表,我不能仅对此发表评论)

首先,据我所知,返回一个urlencode()d字符串,这意味着您将没有[and],而是有%5B和%5D

其次,PHP的PCRE引擎将“[”字符识别为字符类的开头,而不仅仅是简单的“[”()。这可能最终会将请求中的所有数字替换为“[]”

您可能更想要这样的东西:

text[]=Hello%20World&text[]=How%20are%20you?&html[]=<p>Just%20fine,%20thank%20you</p>
preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($params));
在这种情况下,您需要对%字符进行转义,因为这些字符也有特殊含义。如果您有一个带实际括号的字符串,而不是转义符,请尝试以下操作:

preg_replace('/\[\d+\]/', '[]', $http_query);

我修复了您发布的示例,现在它只适用于对
[]
进行编码。不仅对空格进行编码,最好在loopurlencode中使用urlencode。urlencode将替换
[]
使用
%5B%5D%3D
-根据上面提供的示例,这不是预期的输出。@RiaD好吧,这就是你的解决方案:))想法就在这里,但这是正确的实现(请参阅php在线手册的注释):preg_replace('/%5B[0-9]+%5D/simU','%5B%5D',$http_query);同意@ErdalG。您还可以执行以下操作:
$httpQuery=http\u build\u query($params,null,'&');$httpquerydecode=urldecode($httpQuery);$requestDataFormatted=urlencode(preg\u replace('\[\d]+\]\\\\\\\\\\\\\\','[',$httpquerydecode));