Session Array In PHP can store more items or data in a single variable but these are not available in different pages for use. Any ordinary ( or normal ) array will loose its data as page execution ends. We have to transfer the array each time the visitor moves away from one page to other. In some applications we need to retain the data as long as the visitor is active at site. Shopping cart is the best example for this. Visitor moves between different pages and adds items to the shopping cart. We have to carry the shopping cart to all the pages along with the visitor. Visitor can add or remove items from the cart from any page.
Today, I will teach you how to create a session array in Hypertext Preprocessor (PHP). Session Array() variable has the ability to hold multiple data at once. This will help you store various data that can be accessed in different pages.
Let’s begin:
First, you have to start the session to read all the session variable that are being declared.
[php]
session_start();
[/php]
Second, declare a SESSION ARRAY variable to be able to store multiple data.
[php]
// It represent a container of data.
$_SESSION[‘cointainer’] = array();
[/php]
Lastly is storing data in the session variable that you have declared.
[php]
$_SESSION[‘cointainer’][0][‘Firstname’]=”Janobe”;
$_SESSION[‘cointainer’][0][‘Lastname’]=”Palacios”;
$_SESSION[‘cointainer’][1][‘Address’]=”Kabankalan City”;
$_SESSION[‘cointainer’][1][‘Email’]=”[email protected]”;
// and you can also do this one.
$_SESSION[’employee’] = array(“Firstname”=>”Janobe”, “Lastname”=>”Palacios”, “Address”=>”Kabankalan”, “Email”=>”[email protected]”);
[/php]
Output :
[php]
//It presents the value and their curresponding keys.
echo print_r($_SESSION[‘cointainer’]) .’
;
echo print_r($_SESSION[’employee’]);
[/php]