Sample code
<?php
session_start();
$_SESSION['admin']=array('adid'=>1,'adname'=>'main admin','privilage'=>'super','lastlogin'=>'12-May-2008');
?>
The above code will assign the array values to the session named 'admin'.session_start();
$_SESSION['admin']=array('adid'=>1,'adname'=>'main admin','privilage'=>'super','lastlogin'=>'12-May-2008');
?>
To view the array content
<?php
echo "<pre>";
print_r($_SESSION['admin']);
echo "</pre>";
// To get a particular element
echo "<pre>";
print_r($_SESSION['admin']['adname']);
echo "</pre>";
?>
echo "<pre>";
print_r($_SESSION['admin']);
echo "</pre>";
// To get a particular element
echo "<pre>";
print_r($_SESSION['admin']['adname']);
echo "</pre>";
?>
To get all the values from the session array. This loop will list all elements in the session array.So we can use all these values using only one session.
<?php
foreach($_SESSION['admin'] as $key=>$value)
{
// it will print out the values
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
?>
foreach($_SESSION['admin'] as $key=>$value)
{
// it will print out the values
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
?>
The output is :
The value of $_SESSION['adid'] is '1'
The value of $_SESSION['adname'] is 'main admin'
The value of $_SESSION['privilage'] is 'super'
The value of $_SESSION['lastlogin'] is '12-May-2008'
The value of $_SESSION['adname'] is 'main admin'
The value of $_SESSION['privilage'] is 'super'
The value of $_SESSION['lastlogin'] is '12-May-2008'