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

明确起见,我有一个具有多个值的查询字符串,其中一个值将是数组值。我希望查询字符串值被视为数组-我不希望数组被分解,以便它与其他查询字符串变量难以区分。

另外,根据这篇文章的回答,作者建议不定义对数组的查询字符串支持。这准确吗?

编辑:

基于@Alex的回答,没有标准的方法来做到这一点,所以我的后续是什么是一个简单的方法来识别我正在阅读的参数是一个数组在PHP和Javascript?

将多个参数命名为相同的名称是否可以接受,这样我就知道它们属于一个数组?例子:

?myarray=value1&myarray=value2&myarray=value3...

或者这是一种不好的做法?


当前回答

我不认为有一个标准。 每个网络环境都为这些事情提供了自己的“标准”。此外,url通常太短(在某些浏览器上限制为256字节)。当然,更长的数组/数据可以通过POST请求发送。

但是,有一些方法:

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/3787376 Object 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.

尽管“方括号方法”简单且有效,但它仅限于PHP和数组。 如果需要其他类型的变量,如类或在非PHP语言的查询字符串中传递变量,则建议使用JSON方法。

PHP中的JSON方法示例(方法2):

$myarray = array(2, 46, 34, "dfg");
$serialized = json_encode($myarray)
$data = 'myarray=' . rawurlencode($serialized);
// Send to page via cURL, header() or other service.

接收页面代码(PHP):

$myarray = json_decode($_GET["myarray"]); // Or $_POST["myarray"] if a post request.

其他回答

我不认为有一个标准。 每个网络环境都为这些事情提供了自己的“标准”。此外,url通常太短(在某些浏览器上限制为256字节)。当然,更长的数组/数据可以通过POST请求发送。

但是,有一些方法:

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/3787376 Object 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.

尽管“方括号方法”简单且有效,但它仅限于PHP和数组。 如果需要其他类型的变量,如类或在非PHP语言的查询字符串中传递变量,则建议使用JSON方法。

PHP中的JSON方法示例(方法2):

$myarray = array(2, 46, 34, "dfg");
$serialized = json_encode($myarray)
$data = 'myarray=' . rawurlencode($serialized);
// Send to page via cURL, header() or other service.

接收页面代码(PHP):

$myarray = json_decode($_GET["myarray"]); // Or $_POST["myarray"] if a post request.

我使用React和Rails。我做了:

js

  let params = {
    filter_array: ['A', 'B', 'C']
  }

  ...

  //transform params in URI

  Object.keys(params).map(key => {
    if (Array.isArray(params[key])) {
      return params[key].map((value) => `${key}[]=${value}`).join('&')
    }
  }
  //filter_array[]=A&filter_array[]=B&filter_array[]=C

这对我来说很管用:

在link中,to属性具有值:

to="/filter/arr?fruits=apple&fruits=banana"

Route可以处理这个:

path="/filter/:arr"

对于多个数组:

to="filter/arr?fruits=apple&fruits=banana&vegetables=potato&vegetables=onion"

路线不变。

截图

更新: 为了实现这种字符串结构,query-string是最好的包。

例如:

import { stringify, parse } from 'query-string';

const queryParams={
   fruits:['apple','banana'],
   vegetables:['potato','onion']
}
//arrayFormat can be bracket or comma 
stringify(queryParams, { arrayFormat: 'bracket' });

您可以使用http_build_query从PHP中的数组生成url编码的查询字符串。当结果查询字符串将被展开时,你可以决定一个唯一的分隔符作为http_build_query方法的参数,因此当涉及到解码时,你可以检查使用了什么分隔符。如果它是你选择的唯一的一个,那么它将是数组querystring,否则它将是普通的querystring。

检查parse_string函数http://php.net/manual/en/function.parse-str.php

它将返回查询字符串中的所有变量,包括数组。

来自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

?>