note 78603 deleted from function.http-build-query by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:37:54 +0000 |
| Subject: | note 78603 deleted from function.http-build-query by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189777@lists.php.net to get a copy of this message | ||
Note Submitter: donovan jimenez
----
Other languages (in my case Java) allow access to multiple values for the same GET/POST parameter
without the use of the brace ([]) notation. I wanted to use this function because it was faster
than my own PHP implementation that urlencoded each part of a parameter array, but I needed to be
able to send something like this to a service:
...?q=foo&q=bar
obviously http_build_query() by itself would have given me:
...?q[0]=foo&q[1]=bar
So, preg_replace to the rescue:
...
$query_string = http_build_query($params);
$query_string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=',
$query_string);
...
And I get single dimension arrays encoded how I need them. This works because the '='
character can't appear non-urlencoded except for exactly where I expect it to appear (between
key / value pairs).
DISCLAIMER: this workaround was only intended for getting rid of the array notation when a parameter
had multiple "simple" values. More complex structures will probably be mangled.