php 如何在php中通过$_GET传递数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7206978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:16:01  来源:igfitidea点击:

How to pass an array via $_GET in php?

phparraysget

提问by Mirko

How can I pass one or more variables of type array to another page via $_GET?

如何通过 $_GET 将一个或多个数组类型的变量传递到另一个页面?

I always passed variable values in the form ?a=1&b=2&c=3

我总是在表单中传递变量值 ?a=1&b=2&c=3

What about passing a=[1,2,3]?

过关a=[1,2,3]怎么办?

Do I need to write a for loop and append all the values?

我是否需要编写一个 for 循环并附加所有值?

Thanks

谢谢

回答by Arnaud Le Blanc

You can use the []syntax to pass arrays through _GET:

您可以使用[]语法通过 _GET 传递数组:

?a[]=1&a[]=2&a[]=3

PHP understands this syntax, so $_GET['a']will be equal to array(1, 2, 3).

PHP 理解这种语法,因此$_GET['a']将等于array(1, 2, 3).

You can also specify keys:

您还可以指定键:

?a[42]=1&a[foo]=2&a[bar]=3

Multidimentional arrays work too:

多维数组也适用:

?a[42][b][c]=1&a[foo]=2

http_build_query()does this automatically:

http_build_query()自动执行此操作:

http_build_query(array('a' => array(1, 2, 3))) // "a[]=1&a[]=2&a[]=3"

http_build_query(array(
    'a' => array(
        'foo' => 'bar',
        'bar' => array(1, 2, 3),
     )
)); // "a[foo]=bar&a[bar][]=1&a[bar][]=2&a[bar][]=3"


An alternative would be to pass json encoded arrays:

另一种方法是传递 json 编码的数组:

?a=[1,2,3]

And you can parse awith json_decode:

你可以解析ajson_decode

$a = json_decode($_GET['a']); // array(1, 2, 3)

And encode it again with json_encode:

并用 json_encode 再次编码:

json_encode(array(1, 2, 3)); // "[1,2,3]"


Dont ever use serialize()for this purpose. Serialize allows to serialize objects, and there is ways to make them execute code. So you should never deserialize untrusted strings.

永远不要serialize()用于此目的。Serialize 允许序列化对象,并且有一些方法可以让它们执行代码。所以你永远不应该反序列化不受信任的字符串。

回答by Michael Mior

You can pass an associative array to http_build_query()and append the resulting string as the query string to the URL. The array will automatically be parsed by PHP so $_GETon the receiving page will contain an array.

您可以将关联数组传递http_build_query()给 URL,并将结果字符串作为查询字符串附加到 URL。该数组将被 PHP 自动解析,因此$_GET在接收页面上将包含一个数组。

Example

例子

$query_str = http_build_query(array(
    'a' => array(1, 2, 3)
));

回答by IRSHAD

$city_names = array(
    'delhi',
    'mumbai',
    'kolkata',
    'chennai'
);
$city_query = http_build_query(array('city' => $city_names));

this will give you:

这会给你:

city[0]=delhi&city[1]=mumbai&city[2]=kolkata&city[3]=chennai

if you want to encode the brackets also then use the below code:

如果您还想对括号进行编码,请使用以下代码:

$city_query = urlencode(http_build_query(array('city' => $city_names)));

Output:

输出:

city%255B0%255D%3Ddelhi%26city%255B1%255D%3Dmumbai .....

Reference: http_build_query, urlencode

参考:http_build_queryurlencode

回答by user2253826

Just repeat your $_GETvariables like this: name=john&name=lea

只需$_GET像这样重复您的变量:name=john&name=lea

This gives you an array.

这给你一个array.

I used to believe it would be overwritten!

我曾经相信它会被覆盖!