Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an array within a query string?

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 parameter 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 a bad practice?

like image 845
Yarin Avatar asked Jun 05 '11 13:06

Yarin


People also ask

Can I pass array in query params?

The same way there is no concensus over how objects should be represented in query parameters, there is no standardized way to format arrays of values in query parameters.

Can we pass array in query string Javascript?

We can also pass an array with tuples or a query string. With that done, we now have an instance of the URLSearchParams class. We can get the string version of this by calling toString and append this to our URL.

Can we pass array in string?

Answer: No! Java is always pass-by-value. Note that Java arrays are reference data types thus, they are non-primitive data types.


2 Answers

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:

<form>      <select multiple="multiple" name="cars[]">          <option>Volvo</option>          <option>Saab</option>          <option>Mercedes</option>      </select> </form> 

(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:

<input type="hidden" name="cars[]" value="Volvo"> <input type="hidden" name="cars[]" value="Saab"> <input type="hidden" name="cars[]" value="Mercedes"> 

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:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3 

Rails:

"list_a": "3",  "list_b":[     "1",     "2",     "3"   ],  "list_c": "1,2,3" 

Angular:

 "list_a": [     "1",     "2",     "3"   ],   "list_b[]": [     "1",     "2",     "3"   ],   "list_c": "1,2,3" 

(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.

like image 155
Yarin Avatar answered Oct 06 '22 12:10

Yarin


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.

like image 44
Alex K. Avatar answered Oct 06 '22 12:10

Alex K.