How to create a Session Array() variable in PHP

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.

session array variable  
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.




Second, declare a SESSION ARRAY variable to be able to store multiple data.

// It represent a container of data.
$_SESSION[‘cointainer’] = array();

Lastly is storing data in the session variable that you have declared.



$_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]”);

Output :

//It presents the value and their curresponding keys.
echo print_r($_SESSION[‘cointainer’]) .’

echo print_r($_SESSION[’employee’]);


Leave a Comment