如何在 PHP 中的 GET 中传递数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5098397/
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
how to pass an array in GET in PHP?
提问by Yoni
$idArray = array(1,2,3,4);
can I write this line in HTML?
我可以用 HTML 写这一行吗?
<form method='POST' action='{$_SERVER['PHP_SELF']}?arr={$idArray}'>
or should I write:
或者我应该写:
<form method='POST' action='{$_SERVER['PHP_SELF']}?arr[]={$idArray}'>
how will it be passed?
它将如何通过?
how should I handle it in the called page?
我应该如何在被调用的页面中处理它?
thanks !!
谢谢 !!
回答by Felix Kling
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 serializeand unserializethe 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.
但是你真的需要再次将数据发送给客户端吗?看起来您只需要一种方法来在请求之间持久化数据。
In this case, it is better imo to use sessions(docs). This is also more secure as otherwise the client could modify the data.
在这种情况下,最好使用session (docs)imo 。这也更安全,否则客户端可以修改数据。
回答by Svisstack
Use serialize
and unserialize
PHP function.
This function giving you storable (string) version of array type.
For more infomation about usage read
http://php.net/manual/en/function.serialize.phpand http://www.php.net/manual/en/function.unserialize.php
使用serialize
和unserialize
PHP函数。此函数为您提供数组类型的可存储(字符串)版本。有关用法的更多信息,请阅读
http://php.net/manual/en/function.serialize.php和http://www.php.net/manual/en/function.unserialize.php
回答by Czechnology
Another option (even nice looking, I'd say):
另一种选择(即使看起来很漂亮,我会说):
<form method='POST'>
<input type="hidden" name="idArray[]" value="1" />
<input type="hidden" name="idArray[]" value="2" />
<input type="hidden" name="idArray[]" value="3" />
<input type="hidden" name="idArray[]" value="4" />
</form>
But of course it gets sent as POST. I wouldn'r recommend sending it with serialize since the output of that function can get pretty big and the length or URL is limited.
但当然它会作为 POST 发送。我不建议使用序列化发送它,因为该函数的输出可能会变得非常大并且长度或 URL 是有限的。
with GET:
使用 GET:
<form method='GET'>
<input type="hidden" name="idArray[]" value="1" />
<input type="hidden" name="idArray[]" value="2" />
<input type="hidden" name="idArray[]" value="3" />
<input type="hidden" name="idArray[]" value="4" />
</form>
回答by mukulu
You could use serialize and & serialize along side with urlencode e.g.
您可以将序列化和 & 序列化与 urlencode 一起使用,例如
On Sending you can send them like these:
在发送时,您可以像这样发送它们:
<?php
$array1 = Array(["key1"]=>"value1",["key2"]=>"value2");
$array2 = Array(["key1"]=>"value1",["key2"]=>"value2");
$data1="textdata";
$urlPortion= '&array1='.urlencode(serialize($array1)).
'&array2='.urlencode(serialize($array2)).
'&data1='.urlencode(serialize($data1));
//Full URL:
$fullUrl='http://localhost/?somevariable=somevalue'.$urlPortion
?>
On Receiving you can access them as:
在接收时,您可以通过以下方式访问它们:
<?php
$destArray1=unserialize($_GET['array1']);
$destArray2=unserialize($_GET['array2']);
$destData1=unserialize($_GET['data1']);
?>
And Voila, you can attach that url on ajax request or normal browser page.
瞧,您可以在 ajax 请求或普通浏览器页面上附加该 url。
回答by Abhishek Saini
Just use explode()
and pass it's value. You can get the array back by using implode().
只需使用explode()
并传递它的值。您可以使用implode()取回数组。
Note: Choose the delimiter according to the type of content that does not exist in your array. For eg. If you are sure that there won't be any commas ( , ) in your array, then pick comma as delimiter.
注意:根据您的阵列中不存在的内容类型选择分隔符。例如。如果您确定数组中没有任何逗号 ( , ),则选择逗号作为分隔符。
回答by nixis
Use parse_str function
使用 parse_str 函数
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;
回答by Stofke
http://snipplr.com/view/4444/passing-an-array-through-get-request/
http://snipplr.com/view/4444/passing-an-array-through-get-request/
$str=serialize($idArray);
<form method='POST' action='{$_SERVER['PHP_SELF']}?arr=$str'>
To get the data in the receiving page you will first have to:
要在接收页面中获取数据,您首先必须:
<?PHP
$idArray = unserialize($_GET["arr"]);
?>
回答by Linus Juhlin
Felix's answeranswers the question beautifully, but lacks the examples in my opinion.
Felix 的回答很好地回答了这个问题,但在我看来缺乏例子。
This answer is prompted by the comment on Felix's answer.
这个答案是由对 Felix 答案的评论提示的。
can you specify keys using this method? – Qwerty Apr 27 '14 at 0:05
你能用这个方法指定键吗?– Qwerty 2014 年 4 月 27 日 0:05
First off, to illustrate Felix's answer:
首先,为了说明菲利克斯的回答:
<input type="hidden" name="array[]" value="val1" />
<input type="hidden" name="array[]" value="val2" />
<input type="hidden" name="array[]" value="val3" />
When the request is sent to the server it will be of the type Array
.
当请求发送到服务器时,它将是 类型Array
。
Following is an example with keys. This will give you an array with two keys, two values each.
下面是一个带键的例子。这将为您提供一个包含两个键的数组,每个键两个值。
<input type="hidden" name="array['first']" value="val1" />
<input type="hidden" name="array['first']" value="val2" />
<input type="hidden" name="array['second']" value="val3" />
<input type="hidden" name="array['second']" value="val4" />
Finally here's an example with VueJS, which is what I was using as of this writing, which led me to this question.
最后,这是一个 VueJS 的例子,这是我在撰写本文时使用的,这让我想到了这个问题。
<input v-for="(value, key) in data" type="hidden" :name="'array[' + key + ']'" :value="value">
I hope this will be helpful to any passersby.
希望对各位路人有所帮助。
回答by infinigrove
Another option is to json_encodethen base64_encodethen urlencodethen you can pass that into a get request.
另一种选择是json_encode然后base64_encode然后urlencode然后你可以将它传递到一个 get 请求中。
$idArray = [1,2,3,4];
$urlArray = urlencode(base64_encode(json_encode($idArray)));
$fullURL = 'https://myserver/mypath/myscript.php?arr=' . $urlArray;
On receiving you can get back to the original array by urldecodethen base64_decodethen json_decode.
在接收时,您可以通过urldecode然后base64_decode然后json_decode返回到原始数组。
$idArray = json_decode(base64_decode(urldecode($_GET["arr"])));
As other have mentioned you can use serializeand unserializebut it is considered to be more secure to use json_encodeand json_decodeinstead. Also as of PHP7 unserializehas a second parameter, for more info see https://github.com/kalessil/phpinspectionsea/blob/master/docs/security.md#exploiting-unserialize
正如其他人提到的,您可以使用序列化和反序列化,但使用json_encode和json_decode被认为更安全。从 PHP7开始,反序列化还有第二个参数,有关更多信息,请参见https://github.com/kalessil/phpinspectionsea/blob/master/docs/security.md#exploiting-unserialize
You may not need to use the base64_encodeand base64_decodebut I recommend it. It will cost you some processing resources but will result in a shorter URL saving you network resources. Keep in mind that if your working with large arrays you may excede the limits of the allowed length of get requests for your server.
您可能不需要使用base64_encode和base64_decode但我推荐它。这将花费您一些处理资源,但会导致更短的 URL 节省您的网络资源。请记住,如果您使用大型数组,您可能会超出服务器获取请求的允许长度限制。
回答by Mhaddy
A session is a much safer and cleaner way to do this. Start your session with:
会话是一种更安全、更干净的方式来做到这一点。开始您的会话:
session_start();
Then add your serialized array as a session variable like this:
然后将您的序列化数组添加为会话变量,如下所示:
$_SESSION["venue"] = serialize($venue);
The simply call up the session variable when you need it.
只需在需要时调用会话变量即可。