5

我有一个包含一些值的数组,比如说

arr['one'] = "one value here";
arr['two'] = "second value here";
arr['three'] = "third value here";

我这个值在页面 home.php 中,在页面末尾它被重定向到页面 detail.php 现在我想在直接发生时将这个数组从页面 home.php 传递到 detail.php。

我可以通过多少种方式使用 post 和 get 方法发送这个值。另外,如果可能的话,请告诉我如何在 detail.php 页面中接收和打印这些值。

非常感谢每种类型的示例。

4

4 回答 4

4

最简单的方法是使用会话将数组从一个页面存储到另一个页面:

session_start();
$_SESSION['array_to_save'] = $arr;

有关会议的更多信息:http: //php.net/manual/en/function.session-start.php

如果你不想使用会话,你可以在你的第一页做这样的事情

$serialized =htmlspecialchars(serialize($arr));
echo "<input type=\"hidden\" name=\"ArrayData\" value=\"$serialized\"/>";

在另一个中,您可以像这样检索数组数据:

$value = unserialize($_POST['ArrayData']);

在这里找到解决方案:https ://stackoverflow.com/a/3638962/1606729

于 2012-12-10T23:56:50.967 回答
3

如果您不想使用会话,则可以将页面包含在另一个文件中。

文件1.php

<php
    $arr = array();
    $arr['one'] = "one value here";
    $arr['two'] = "second value here";
    $arr['three'] = "third value here";
?>

文件2.php

<?php

    include "file1.php";

    print_r($arr);
?>

如果数组是动态创建的并且您想通过 GET 或 POST 传递它,您应该在服务器端形成 URL 并将用户重定向到 HTTP URL 页面而不是 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" );

?>

文件2.php

<?php

    $params = $_GET;

    print_r($params['one']);
    print_r($params['two']);
    print_r($params['three']);
?>
于 2012-12-11T00:03:41.043 回答
2

home.php 文件

session_start();
if(!empty($arr)){
    $_SESSION['value'] = $arr;
     redirect_to("../detail.php");
}

详细信息.php

session_start();                    
if(isset($_SESSION['value'])){                           
    foreach ($_SESSION['value'] as $arr) {
        echo $arr . "<br />";
        unset($_SESSION['value']);
    }
}
于 2012-12-11T00:48:49.520 回答
0

您也可以通过查询参数传递值。

header('Location: detail.php?' . http_build_query($arr, null, '&'));

您可以像这样在 detail.php 中获取数组:

// your values are in the $_GET array
echo $_GET['one'];  // echoes "one value here" by your example

请注意,如果您通过 GET 或 POST(隐藏输入字段)传递值,用户可以轻松更改它们。

于 2012-12-11T00:11:28.620 回答