如何在 PHP 中的 GET 中传递数组? [英] how to pass an array in GET in PHP?

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

问题描述

$idArray = array(1,2,3,4);

我可以用 HTML 写这一行吗?

can I write this line in HTML?

<form method='POST' action='{$_SERVER['PHP_SELF']}?arr={$idArray}'>

或者我应该写:

<form method='POST' action='{$_SERVER['PHP_SELF']}?arr[]={$idArray}'>

如何通过?

如何在被调用的页面中处理?

how should I handle it in the called page?

谢谢!!

推荐答案

如果要将数组作为参数传递,则必须为每个元素添加一个参数.您的查询字符串将变为:

If you want to pass an array as parameter, you would have to add a parameter for each element. Your query string would become:

?arr[]=1&arr[]=2&arr[]=3&arr[]=4

正如其他人所写,您也可以序列化反序列化数组.

As others have written, you can also serialize and unserialize the array.

但是你真的需要再次将数据发送给客户端吗?看起来您只需要一种方法来在请求之间保持数据.

But do you really have to send the data to the client again? It looks like you just need a way to persist the data between requests.

在这种情况下,最好使用 sessions(文档).这也更安全,否则客户端可以修改数据.

In this case, it is better imo to use sessions(docs). This is also more secure as otherwise the client could modify the data.

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

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