php数组传给其他页面
时间: 2023-08-16 20:30:16 浏览: 47
要将 PHP 数组传递给其他页面,可以使用以下方法:
1. 将数组序列化为字符串,然后在 URL 参数或表单隐藏字段中传递。在接收页面中,可以使用 unserialize() 函数将字符串还原为数组。
示例代码:
```
// 在传递页面中
$arr = array('foo' => 'bar', 'baz' => 'qux');
$data = base64_encode(serialize($arr));
$url = 'http://example.com/receive.php?data=' . $data;
// 或者
echo '<form method="post" action="receive.php">';
echo '<input type="hidden" name="data" value="' . $data . '">';
echo '<input type="submit" value="Submit">';
echo '</form>';
// 在接收页面中
$data = $_GET['data']; // 或者 $_POST['data']
$arr = unserialize(base64_decode($data));
print_r($arr);
```
2. 使用会话变量(Session)传递数组。在传递页面中,将数组赋值给会话变量,然后在接收页面中,可以直接从会话变量中获取数组。
示例代码:
```
// 在传递页面中
session_start();
$arr = array('foo' => 'bar', 'baz' => 'qux');
$_SESSION['data'] = $arr;
header('Location: http://example.com/receive.php');
// 在接收页面中
session_start();
$arr = $_SESSION['data'];
print_r($arr);
```
注意:使用会话变量需要确保会话已经启动,即在传递页面中调用 session_start() 函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)