PHP http_build_query()为数组get参数生成不正确的url

I probably discovered a bug of a PHP function http_build_query().

I am developing a search engine with dynamic form and some of these parameters are array.

I simply get the query url from the current $_GET with http_build_query(). But all of these array parameters are automatically changed from "arrayName%5B%5D" to "arrayName%5B0%5D" in the new query generated.

$queryStr = http_build_query($_GET);

original url

&arrayName%5B%5D=

new query string got from http_build_query():

&arrayName%5B0%5D=

What is the reason for this? How to fix this?

It's not a bug to http_build_query() function.

When you pass the get parameter via URL like: "arrayName[]="

print_R($_GET);

will return

Array
(
    [arrayName] => Array
        (
            [0] => 
        )

)

and http_build_query generates the url encoded string from array, the result is:

&arrayName%5B0%5D=

and decoded this looks like:

arrayName[0]=

Now you can see where the 0 came from :)

There is no need to fix this, you can change your code to pass keys for the arrayName or still use it as is.