在這裏你去。首先開始一個會話。然後創建數組(如果不存在於會話數組中)。添加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」? –