在这里你去。首先开始一个会话。然后创建数组(如果不存在于会话数组中)。添加inputName
命名字段的值。第二个是检查,该值是否已经在数组中,如果是,则不要再次添加。
稍后,您可以在任何页面上访问$_SESSION['myArray']
。
不要忘记在你使用它的地方每个页面开始会话session_start();
。
<?php
session_start();
//If session array not exists create one
if (empty($_SESSION['myArray'])) {
$_SESSION['myArray'] = array();
}
if (!empty($_GET['inputName'])) {
$_SESSION['myArray'][] = $_GET['inputname'];
}
//If want to check is the value of inputName exists, and add only once:
if (!empty($_GET['inputName']) && !in_array($_GET['inputName'], $_SESSOION['myArray'])) {
$_SESSION['myArray'][] = $_GET['inputname'];
}
?>
HTML starts here.
像[数组合并](http://php.net/manual/en/function.array-merge.php)? – Chay22
或'array_push' ..? [链接](http://php.net/manual/en/function.array-push.php) – Stuart
或按数组键名$ a ['k_name'] =“value”? –