PHP - 如何将数组发送到另一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1548159/
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
PHP - How to send an array to another page?
提问by Mike
I'm trying to send an array to another page.
我正在尝试将数组发送到另一个页面。
What I tryed before was:
我之前尝试的是:
page1
第1页
<input type='hidden' name='input_name' value='<?php print_r($array_name); ?>' />
And page2
和第2页
<?php
$passed_array = $_POST['input_name'];
?>
Now how do I make $passed_arrayact like an array?
现在我如何使$passed_array行为像一个数组?
Or do you know of any other way of solving this problem?
或者你知道解决这个问题的任何其他方法吗?
Thanks, Mike.
谢谢,迈克。
Edit: The reason I want to do it this way is because I need to avoid sessions and cookies.
编辑:我想这样做的原因是因为我需要避免会话和 cookie。
回答by Greg
You could put it in the session:
你可以把它放在会话中:
session_start();
$_SESSION['array_name'] = $array_name;
Or if you want to send it via a form you can serializeit:
<input type='hidden' name='input_name' value="<?php echo htmlentities(serialize($array_name)); ?>" />
$passed_array = unserialize($_POST['input_name']);
The session has the advantage that the client doesn't see it (therefore can't tamper with it) and it's faster if the array is large. The disadvantage is it could get confused if the user has multiple tabs open.
会话的优点是客户端看不到它(因此不能篡改它),并且如果数组很大,它会更快。缺点是如果用户打开多个选项卡可能会感到困惑。
Edit: a lot of answers are suggesting using name="input_name[]". This won't work in the general case - it would need to be modified to support associative arrays, and modified a lot to support multidimensional arrays (icky!). Much better to stick to serialize.
编辑:很多答案都建议使用name="input_name[]". 这在一般情况下不起作用 - 它需要修改以支持关联数组,并进行大量修改以支持多维数组(icky!)。坚持序列化要好得多。
回答by Martijn Heemels
You could serialize the array, which turns it into a string, and then unserialize it afterwards, which turns it back into an array. Like this:
您可以序列化 array,将其转换为字符串,然后将其反序列化,从而将其转换回数组。像这样:
<input type='hidden' name='input_name' value='<?php serialize($array_name); ?>' />
and on page 2:
在第 2 页:
<?php $passed_array = unserialize($_POST['input_name']); ?>
回答by AlBeebe
I ran into some issues with the above examples when some values in my array contained line breaks. Some of my values also had characters from foreign languages which htmlentitieskept screwing up. The following was my solution.
当数组中的某些值包含换行符时,我遇到了上述示例的一些问题。我的一些价值观也包含来自外语的字符,这些字符htmlentities一直在搞砸。以下是我的解决方案。
In the page that you want to pass the array from...
在要从中传递数组的页面中...
<INPUT TYPE="hidden" NAME="input_name" VALUE="<?= base64_encode(serialize($array_name)); ?>">
In the page that receives the array...
在接收数组的页面中...
$array = unserialize(base64_decode($_POST["input_name"]));
回答by Scott Saunders
You can not send the array all at once, you will have to either send each value individually:
您不能一次发送所有数组,您必须单独发送每个值:
<input type='hidden' name='input_name[]' value='<?php print_r($array_name[0]); ?>' />
<input type='hidden' name='input_name[]' value='<?php print_r($array_name[1]); ?>' />
<input type='hidden' name='input_name[]' value='<?php print_r($array_name[2]); ?>' />
...
Or look into jsonor serialization.
或查看json或序列化。
回答by Nelson Emeka Ameyo
You can simply json_encode()the array then pass it as a string in the POST request. Used it many times. Works everytime like a young titty
您可以简单地json_encode()将数组作为字符串传递给 POST 请求。多次使用它。每次都像年轻的奶子一样工作
回答by Pekka
Note that to work with serialized arrays, you need to use POST as the form's transmission method, as GET has a size limit somewhere around 1024 characters.
请注意,要使用序列化数组,您需要使用 POST 作为表单的传输方法,因为 GET 有大约 1024 个字符的大小限制。
I'd use sessions wherever possible.
我会尽可能使用会话。
回答by Mike
<?php
/*arraytransfer.php*/
echo "Array transfer<br>";
$name = array( "mike", "tom" );
$arrCnt = sizeof( $name );
echo "arrCnt: $arrCnt<br>";
echo "<form action=\"arrayrcv2.php\" method=\"POST\">";
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"arrCnt\" VALUE=\"$arrCnt\">";
for( $i=0; $i < $arrCnt; $i++ ) {
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"name\" VALUE=\"$name[$i]\"> ";
}
echo "<input type=\"submit\" name=\"submit\" value=\"Submit me!\" />";
echo "</form>";
?>
<?php
/*arrayrecv.php */
$arrCnt = $_POST["arrCnt"];
echo "Receiving data arrCnt = $arrCnt<br>";
$name = array();
for( $i = 0; $i < $arrCnt; $i++ ) {
$var = $_POST["name"];
if( $var != "" ) array_push($name, $var );
}
print_r($name);
?>
回答by Egor Pavlikhin
Change input_name to input_name[] in your input tag, then put an input tag for every value of the array.
在输入标签中将 input_name 更改为 input_name[],然后为数组的每个值放置一个输入标签。
http://phpprogramming.wordpress.com/2007/05/06/php-passing-array-using-hidden-form-element/
http://phpprogramming.wordpress.com/2007/05/06/php-passing-array-using-hidden-form-element/

