Skip to content
Advertisement

Passing arrays as url parameter

What is the best way that I can pass an array as a url parameter? I was thinking if this is possible:

JavaScript

or how about this:

JavaScript

Ive read examples, but I find it messy:

JavaScript

Advertisement

Answer

There is a very simple solution: http_build_query(). It takes your query parameters as an associative array:

JavaScript

will return

JavaScript

http_build_query() handles all the necessary escaping for you (%5B => [ and %5D => ]), so this string is equal to aParam[0]=1&aParam[1]=4&aParam[a]=b&aParam[c]=d.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement