session
1.session中的数组
提交页面
<?php //开始session session_start(); if(isset($_POST['products'])){ if(!empty($_SESSION['products'])){ //array_unique数组去重 $products = array_unique( //array_merge合并数组 array_merge( //unserialize将存在session中的数组序列转化为数组 unserialize($_SESSION['products']), $_POST['products'] ) ); //serialize序列化后存储session $_SESSION['products'] = serialize($products); } else { $_SESSION['products'] = serialize($_POST['products']); } echo '将数组插入session完成'; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" > <select name="products[]" multiple> <option value="iPhone">iPhone</option> <option value="ipad">ipad</option> <option value="mac air">mac air</option> </select> <button type="submit">提交</button> </form> <a href="session.php">另一个页面</a> </body> </html>
session.php访问session页面
<?php //开始session session_start(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <?php echo "<ul>"; if(!empty($_SESSION['products'])){ foreach(unserialize($_SESSION['products']) as $value){ echo "<li>".$value."</li>"; } } echo "</ul>"; ?> </body> </html>