-1

I am working on a php web application which have many sessions variable. I want to unset some of them.

I dont want to write many lines code

unset($_SESSION['a']);
unset($_SESSION['b']);
unset($_SESSION['c']);
unset($_SESSION['d']);
unset($_SESSION['f']);
unset($_SESSION['a']);

Is to possible to unset this all by one method ?

Thanks

4

3 回答 3

9

Method 1:

$removeKeys = array('a', 'b', 'c', 'd', 'e');

foreach($removeKeys as $key) {
   unset($_SESSION[$key]);
}

Method 2:

unset($_SESSION['a'], $_SESSION['b'], $_SESSION['c']);
于 2013-11-11T11:30:16.760 回答
4

Unset accepts more that one variable, you could just pass them all in - or create a list like so:-

<?php
$keys = array('a', 'b', 'c', 'd');

foreach ($keys as $key) {
    unset($_SESSION[$key]);
}
于 2013-11-11T11:29:24.820 回答
3

You can group array elements and then do unset to whole group.

$_SESSION['foo']['a'] = 'value_a';
...
$_SESSION['foo']['f'] = 'value_f';

unset($_SESSION['foo']);
于 2013-11-11T11:37:41.210 回答