Jump to content

Delete every session apart from two


stuckwithcode

Recommended Posts

You mean remove all current session variables apart from two..?

 

$save = array('var1', 'var2');

foreach ($_SESSION as $name => $value)
{
    if (!in_array($name, $save))
    {
        unset($_SESSION[$name]);
    }
}

Link to comment
Share on other sites

Hi, I'll explain a little but your best bet is to checkout the manual for how foreach loops are constructed.

 

As you should know, every array has a key and a value. See this array:

 

$something['key1'] = 'val1';

 

is the same as..

 

$something = array('key1' => 'val1');

 

Common foreach loop is:

 

foreach($something as $val){

}

 

 

That feeds the loop the $val for each element of the array. But then we come across the common need for using the $key of an array. So now our foreach uses the pointer in the same fashoin it did in the array() example above.

 

foreach($something as $key => $val){

}

 

 

The confusion here is that the $val is now no longer straight after the 'as' keyword. It is preceded by $key =>

 

I'm not entirely sure of the correct term for this => but it is somekind of array pointer. There is another pointer -> which is used for objects.

Link to comment
Share on other sites

foreach ($array as $key => $val) is the same syntax as when you define an array with keys:

 

$array = array(

    'key' => 'val'

);

 

"=>" is just an (array) assignment operator specific to PHP. As far as I know the only other popular language that uses it is PL/SQL from the Oracle platform, for naming parameters in procedure/function calls.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.