php session array

i need to know how to:-

  1. set up a session array
  2. add items to the array
  3. remove items from array
  4. list items in array

the first thing you need to do to set up sessions you need to start the sessions.
[php]session_start();[/php]

then you cand declare it.

[php]$my_array = array(‘red’, ‘blue’, ‘black’, ‘white’);
$_SESSION[‘colors’] = $my_array;[/php]

To output the sessions array you will need a foreach loop.

[php]foreach($_SESSION[‘colors’] as $key=>$value)
{
echo ‘Value of $_SESSION[’."’".$key."’".’] is ‘."’".$value."’".’’;
}[/php]

To unset the items from array you will need unset();

[php]unset($_SESSION[‘colors’][‘id’]);[/php]
you will get the id from the output or you can declare it in the array.

[php]array(‘id1’ => ‘white’, ‘id2’=>‘black’);[/php]and so on…

Sponsor our Newsletter | Privacy Policy | Terms of Service