如何通过$ _GET传递一个数组在PHP? [英] How to pass an array via $_GET in php?

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

问题描述

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

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

我总是在形式传递变量值 A = 1&安培; B = 2及C = 3

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

有关传递什么 A = [1,2,3]

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

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

感谢

推荐答案

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

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

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

PHP懂得这个语法,所以 $ _ GET ['一'] 将等于阵列(1,2,3)

您也可以指定键:

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

多维数组工作了:

Multidimentional arrays work too:

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

http_build_query() 自动执行此操作:

http_build_query() does this automatically:

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"


另一种方法是通过JSON EN codeD数组:


An alternative would be to pass json encoded arrays:

?a=[1,2,3]

和可以解析 A json_de code

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

和EN code将其再次json_en code:

And encode it again with json_encode:

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


不要永远使用连载()为此。序列化允许对象序列化,并有办法让他们执行code。所以,你不应该反序列化不受信任的字符串。


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.

这篇关于如何通过$ _GET传递一个数组在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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