stuckwithcode Posted December 8, 2010 Share Posted December 8, 2010 Hello, I have many session variables and instead of unsetting them all I want to know if there is some code to remove all current sessions apart from a couple of them. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/221013-delete-every-session-apart-from-two/ Share on other sites More sharing options...
Adam Posted December 8, 2010 Share Posted December 8, 2010 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]); } } Quote Link to comment https://forums.phpfreaks.com/topic/221013-delete-every-session-apart-from-two/#findComment-1144384 Share on other sites More sharing options...
stuckwithcode Posted December 8, 2010 Author Share Posted December 8, 2010 hello that works well thank you, could you explain it a bit though what does the arrow mean. cheers Quote Link to comment https://forums.phpfreaks.com/topic/221013-delete-every-session-apart-from-two/#findComment-1144390 Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221013-delete-every-session-apart-from-two/#findComment-1144430 Share on other sites More sharing options...
Adam Posted December 8, 2010 Share Posted December 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221013-delete-every-session-apart-from-two/#findComment-1144433 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.