Search code examples
phparraysquery-stringquerystringparameter

Getting array param out of query string with PHP


(NOTE: This is a follow up to a previous question, How to pass an array within a query string?, where I asked about standard methods for passing arrays within query strings.)

I now have some PHP code that needs to consume the said query string- What kind of query string array formats does PHP recognize, and do I have to do anything special to retrieve the array?

The following doesn't seem to work:

Query string:

?formparts=[a,b,c]

PHP:

$myarray = $_GET["formparts"];
echo gettype($myarray)

result:

string

Solution

  • Your query string should rather look like this:

    ?formparts[]=a&formparts[]=b&formparts[]=c