note 78603 added to function.http-build-query
| From: | donovanjimenez at osu1 dot php dot net | Date: | Thu, 18 Oct 2007 18:37:15 +0000 |
| Subject: | note 78603 added to function.http-build-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131513@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 209.41.74.194
Probable Submitter: 74.95.169.49
----
Manual Page -- http://www.php.net/manual/en/function.http-build-query.php
Edit -- https://master.php.net/note/edit/78603
Del: integrated -- https://master.php.net/note/delete/78603/integrated
Del: useless -- https://master.php.net/note/delete/78603/useless
Del: bad code -- https://master.php.net/note/delete/78603/bad+code
Del: spam -- https://master.php.net/note/delete/78603/spam
Del: non-english -- https://master.php.net/note/delete/78603/non-english
Del: in docs -- https://master.php.net/note/delete/78603/in+docs
Del: other reasons-- https://master.php.net/note/delete/78603
Reject -- https://master.php.net/note/reject/78603
Search -- https://master.php.net/manage/user-notes.php