如何通过查询字符串中的数组? [英] How to pass an array within a query string?

查看:185
本文介绍了如何通过查询字符串中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有传递数组通过查询字符串的标准方法?

需要明确的是,我有一个查询字符串包含多个值,其中之一将是一个数组值。欲被处理该查询字符串值作为一个阵列 - 我不想使其从另一查询字符串变量区分阵列被分解。

此外,根据本<一href=\"http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript/2880929#2880929\">post回答,笔者建议对数组的查询字符串的支持没有定义。这是准确的?

编辑:

基于@亚历克斯的答案

,有这样做的没有标准的方式,所以我跟进的是那么什么是一个简单的方法来的识别的我正在读paramater在两个数组 PHP 的JavaScript

难道是可以接受的名称的多个参数的方式相同的名字,这样我就知道他们属于一个数组?例如:

  myArray的=值1&放大器; myArray的=值2和放大器; myArray的=值3 ...

或者这会是不好的做法?


解决方案

下面是我想通了:

提交多值表单域,即通过GET / POST瓦尔提交的阵列,可以完成多种不同的方式,作为一个标准不一定拼写出来。

三种可能的方式发送多值字段或数组是:


  • 汽车[] =萨博和放大器;汽车[] =奥迪(最佳方式 - PHP读取到一个数组这一点)

  • 汽车萨博=&功放;汽车=奥迪(错误的方式 - PHP将只登记最后一个值)

  • ?汽车萨博=奥迪(没有试过)

表单例子

在一个形式,多值字段可以采取选择框设置为多形式

 &LT;形式为GT;
    &LT;选择多个=多​​个名=汽车[]&GT;
        &LT;选项&GT;沃尔沃&LT; /选项&GT;
        &LT;选项&GT;萨博&LT; /选项&GT;
        &LT;选项&GT;奔驰&LT; /选项&GT;
    &LT; /选择&GT;
&LT; /表及GT;

(注:在这种情况下,它会命名选择控制是很重要的 some_name [] ,因此所产生的请求,增值经销商将被注册为通过PHP数组)

...或多个隐藏名称相同的字段

 &LT;输入类型=隐藏的名字=汽车[]值=沃尔沃&GT;
&LT;输入类型=隐藏的名字=汽车[]值=萨博&GT;
&LT;输入类型=隐藏的名字=汽车[]值=奔驰&GT;


注意:字段[] 多个值实在是不良记录。我没有看到它的任何提及在查询字符串多值键部分 - 维基百科,或 W3C文档处理多选择输入。


更新

作为评论者所指出的那样,这是很多的框架,具体的。一些例子:

查询字符串:

<$p$p><$c$c>?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

角:

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

(角讨论

看评论中的例子的node.js 字preSS ASP.net


维持秩序:
还有一个要考虑的是,如果你需要保持你的项目的为了的(即数组作为一个有序列表),你真的只有一个选择,这是值传递的分隔列表,并它明确地转换到一个数组自己。

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?

解决方案

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.

这篇关于如何通过查询字符串中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆