php 将数组从一页传递到另一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13811385/
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
Pass array from one page to another
提问by
I have an array containing some values, say
我有一个包含一些值的数组,比如
arr['one'] = "one value here";
arr['two'] = "second value here";
arr['three'] = "third value here";
I this values are in the page home.php and at the end of the page it is redirected to page detail.php Now i want to pass this array from page home.php to detail.php when direct occur.
我这个值在页面 home.php 中,在页面末尾它被重定向到页面 detail.php 现在我想在直接发生时将这个数组从页面 home.php 传递到 detail.php。
In how many ways I can send this value using post and get method. Also if possible show me how to receive and print those values in detail.php page.
我可以通过多少种方式使用 post 和 get 方法发送这个值。另外,如果可能的话,请告诉我如何在 detail.php 页面中接收和打印这些值。
An example of each type is much appreciated.
非常感谢每种类型的示例。
回答by koopajah
The easiest way to do this would be to use the session to store the array from one page to another:
最简单的方法是使用会话将数组从一个页面存储到另一个页面:
session_start();
$_SESSION['array_to_save'] = $arr;
More info on the sessions : http://php.net/manual/en/function.session-start.php
有关会话的更多信息:http: //php.net/manual/en/function.session-start.php
If you don't want to use session you can do something like this in your first page
如果你不想使用 session 你可以在你的第一页做这样的事情
$serialized =htmlspecialchars(serialize($arr));
echo "<input type=\"hidden\" name=\"ArrayData\" value=\"$serialized\"/>";
and in the other one you retrieve the array data like this :
在另一个中,您像这样检索数组数据:
$value = unserialize($_POST['ArrayData']);
Solution found here : https://stackoverflow.com/a/3638962/1606729
解决方案在这里找到:https: //stackoverflow.com/a/3638962/1606729
回答by Kartik
If you dont want to use sessions, you can just include the page in the other file.
如果您不想使用会话,您可以将页面包含在另一个文件中。
file1.php
文件1.php
<php
$arr = array();
$arr['one'] = "one value here";
$arr['two'] = "second value here";
$arr['three'] = "third value here";
?>
file2.php
文件2.php
<?php
include "file1.php";
print_r($arr);
?>
If the array is dynamically created and you want to pass it through GET or POST you should form the URL on the server side and redirect the user to the HTTP URL page instead of the php file.
如果数组是动态创建的并且您想通过 GET 或 POST 传递它,您应该在服务器端形成 URL 并将用户重定向到 HTTP URL 页面而不是 php 文件。
So something like:
所以像:
file1.php
文件1.php
<php
$arr = array();
$arr['one'] = "one value here";
$arr['two'] = "second value here";
$arr['three'] = "third value here";
$redirect = "http://yoursite.com/file2.php?".http_build_query($arr);
header( "Location: $redirect" );
?>
file2.php
文件2.php
<?php
$params = $_GET;
print_r($params['one']);
print_r($params['two']);
print_r($params['three']);
?>
回答by asdfkjasdfjk
home.php file
home.php 文件
session_start();
if(!empty($arr)){
$_SESSION['value'] = $arr;
redirect_to("../detail.php");
}
detail.php
详细信息.php
session_start();
if(isset($_SESSION['value'])){
foreach ($_SESSION['value'] as $arr) {
echo $arr . "<br />";
unset($_SESSION['value']);
}
}
回答by Marius Bal?ytis
You can pass the values by query parameters, too.
您也可以通过查询参数传递值。
header('Location: detail.php?' . http_build_query($arr, null, '&'));
And you can get the array in the detail.php like this:
您可以像这样在 detail.php 中获取数组:
// your values are in the $_GET array
echo $_GET['one']; // echoes "one value here" by your example
Be aware that if you pass values by GET or POST (hidden input field), they can be changed by the user easily.
请注意,如果您通过 GET 或 POST(隐藏输入字段)传递值,则用户可以轻松更改它们。

