如何在查询字符串中传递数组? [英] How to pass an array within a query string?

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

问题描述

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

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?

基于@Alex 的回答,没有标准的方法可以做到这一点,所以我的后续行动是识别我正在读取的参数是一个数组的简单方法是什么PHPJavascript?

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?

推荐答案

这是我想到的:

提交多值表单字段,即通过 GET/POST 变量提交数组,可以通过几种不同的方式来完成,因为不一定要详细说明标准.

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 (最好的方法 - PHP 将其读入数组)
  • ?cars=Saab&cars=Audi (糟糕的方式 - PHP 只会注册最后一个值)
  • ?cars=Saab,Audi (还没试过)

在表单上,​​多值字段可以采用选择框设置为多个的形式:

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>

(注意:在这种情况下,将选择控件命名为 some_name[] 很重要,这样生成的请求变量就会被 PHP 注册为数组)

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

...或作为多个同名隐藏字段:

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

<小时>

注意:field[] 用于多个值的记录确实很差.我在 Query string - Wikipedia 的多值键部分或W3C 文档处理多选输入.


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.

更新

正如评论者所指出的,这是非常特定于框架的.一些例子:

As commenters have pointed out, this is very much framework-specific. Some examples:

查询字符串:

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

导轨:

"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"

(Angular 讨论)

查看 node.jsWordpressASP.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天全站免登陆