php url query nested array with no index

前端 未结 4 1144
清歌不尽
清歌不尽 2021-01-11 15:28

I\'m working with a third party API that receives several parameters which must be encoded like this:

text[]=Hello%20World&text[]=How%20are%20you?&ht         


        
4条回答
  •  一生所求
    2021-01-11 16:27

    Try this:

    $params['text'][] = 'Hello World';
    $params['text'][] = 'How are you?';
    $params['html'][] = '

    Just fine, thank you

    '; 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;

提交回复
热议问题