Is there a standard way of passing an array through a query string?
To be clear, I have a query string with multiple values, one of which would be an array value. I want that query string value to be treated as an array- I don't want the array to be exploded so that it is indistinguishable from the other query string variables.
Also, according to this post answer, the author suggests that query string support for arrays is not defined. Is this accurate?
EDIT:
Based on @Alex's answer, there is no standard way of doing this, so my follow up is then what is an easy way to recognize that the paramater I'm reading is an array in both PHP and Javascript?
Would it be acceptable to name multiple params the same name, and that way I would know that they belong to an array? Example:
?myarray=value1&myarray=value2&myarray=value3...
Or would this be bad practice?
You can use http_build_query to generate a URL-encoded querystring from an array in PHP. Whilst the resulting querystring will be expanded, you can decide on a unique separator you want as a parameter to the
http_build_query
method, so when it comes to decoding, you can check what separator was used. If it was the unique one you chose, then that would be the array querystring otherwise it would be the normal querystrings.I don't think there's a standard.
Each web environment provides its own 'standard' for such things. Besides, the url is usually too short for anything (256 bytes limit on some browsers). Of course longer arrays/data can be send with POST requests.
However, there are some methods:
There's a PHP way, which uses square brackets (
[
,]
) in URL queries. For example a query such as?array_name[]=item&array_name[]=item_2
has been said to work, despite being poorly documented, with PHP automatically converting it into an array. Source: https://stackoverflow.com/a/9547490/3787376Object data-interchange formats (e.g. JSON - official website, PHP documentation) can also be used if they have methods of converting variables to and from strings as JSON does.
Also an url-encoder (available for most programming languages) is required for HTTP get requests to encode the string data correctly.
Although the "square brackets method" is simple and works, it is limited to PHP and arrays.
If other types of variable such as classes or passing variables within query strings in a language other than PHP is required, the JSON method is recommended.
Example in PHP of JSON method (method 2):
Code for receiving page (PHP):
Check the
parse_string
function http://php.net/manual/en/function.parse-str.phpIt will return all the variables from a query string, including arrays.
Example from php.net:
You mention PHP and Javascript in your question, but not in the tags. I reached this question with the intention of passing an array to an MVC.Net action.
I found the answer to my question here: the expected format is the one you proposed in your question, with multiple parameters having the same name.
A query string carries textual data so there is no option but to explode the array, encode it correctly and pass it in a representational format of your choice:
p1=value1&pN=valueN...
data=[value1,...,valueN]
data={p1:value1,...,pN:valueN}
and then decode it in your server side code.
Here's what I figured out:
Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.
Three possible ways to send multi-value fields or arrays would be:
?cars[]=Saab&cars[]=Audi
(Best way- PHP reads this into an array)?cars=Saab&cars=Audi
(Bad way- PHP will only register last value)?cars=Saab,Audi
(Haven't tried this)Form Examples
On a form, multi-valued fields could take the form of a select box set to multiple:
(NOTE: In this case, it would be important to name the select control
some_name[]
, so that the resulting request vars would be registered as an array by PHP)... or as multiple hidden fields with the same name:
NOTE: Using
field[]
for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.UPDATE
As commenters have pointed out, this is very much framework-specific. Some examples:
Query string:
Rails:
Angular:
(Angular discussion)
See comments for examples in node.js, Wordpress, ASP.net
Maintaining order: One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.