Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
What framework are you using? Some frameworks have methods to help pass arrays to querystrings. –  keyboardP Jun 5 '11 at 13:10
    
@keyboardP- PHP and Javascript, depending on use case –  Yarin Jun 5 '11 at 13:31
2  
Why would you want to do that when you could just do:?myarray=value1,value2,value3 –  Serodis Jun 5 '11 at 13:32
4  
@seroids: what if he has to pass commas, question marks, equal signes, and non-printable characters? –  Berry Tsakala Jun 5 '11 at 13:47

8 Answers 8

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.

share|improve this answer
    
OK thanks for clarification. Can you see my revised question- is it necessary to do p1...pn, for keys, or can I just name multiple keys 'p', and that way I would know they all belong to same array? –  Yarin Jun 5 '11 at 13:31
    
You can use the same name; stackoverflow.com/questions/2203430/… or just p=aa,bb,cc which is the most obvious way as Serodis commented. –  Alex K. Jun 5 '11 at 13:35

i don't think there's a standard.

each web environemnt provides its own 'standard' for such things. besiedes, using the url is usually too short for anything (256 bytes limit on some browsers).

there's PHP's way, which uses [square brackets].

you can use an object serializer (e.g. json) and then url-encoder (available for most programming languages),

e.g.

myarray = [2, 46, 34, "dfg"]
serizlized = json(myarray)
url = 'myarr=' + urllib2.urlencode(serialized) 
share|improve this answer

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.

share|improve this answer
up vote 68 down vote accepted

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.

share|improve this answer
2  
I think the reason that this functionality is not documented in general sources like Wiki or W3C is that it is not generally supported by web frameworks. PHP and I think Rails automatically convert multiple "key[]" query parameters into an array. Others don't. –  Carl G Oct 25 '12 at 16:16
1  
The nodejs querystring module uses the ?cars=Saab&cars=Audi form –  SystemParadox Oct 25 '13 at 11:41
5  
In express (node.js), both ?cars=Saab&cars=Audi and ?cars[]=Saab&cars[]=Audi get turned into arrays. However, ?cars=Saab ends up as a string, but ?cars[]=Saab is an array with a single element. –  Trevor Dixon Nov 22 '13 at 18:29
    
In the select, you miss values for options (you have got only labels). I didn't find a spec that would say that value = label if the value attribute is missing. Another thing is that "?cars=Saab&cars=Audi" is not a bad way generally. E.g. in Django (python framework), it is the right way. –  clime Dec 10 '13 at 12:44
    
Can you set keys using this approach? And what about multi-dimensional arrays? –  Qwerty May 23 '14 at 11:43

Check the parse_string function http://php.net/manual/en/function.parse-str.php

It will return all the variables from a query string, including arrays.

Example from php.net:

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

?>
share|improve this answer

I feel it would be helpful for someone who is looking for passing the array in a query string to a servlet. I tested below query string and was able to get the array values using req.getgetParameterValues(); method. Below is the query string I passed through browser.

"http://localhost:8080/ServletsTutorials/*.html?myname=abc&initial=xyz&checkbox=a&checkbox=b"

checkbox is my parameter array here.

share|improve this answer

Nowadays, you can use JSON.

There is multiple library available lke Google-Gson.

share|improve this answer
    
JSON != Query String. –  Cœur Jun 16 '14 at 0:36
    
@Cœur But, there's nothing stopping you from using JSON in a querystring. –  Brad Jun 18 '14 at 0:06
    
@Brad: then there is also [en.wikipedia.org/wiki/Turtle_(syntax) Turtle], CSV, XML, HTML <LI>. It's just encapsulating a different format in a string. –  Cœur Jun 18 '14 at 9:12

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.