Is it possible to unset $_SESSION
variables using a foreach
cycle? This is an example.
是否可以使用foreach循环取消设置$ _SESSION变量?这是一个例子。
<?php
session_start();
$_SESSION['one'] = 'one';
$_SESSION['two'] = 'two';
$_SESSION['three'] = 'three';
print_r($_SESSION);
foreach($_SESSION as $value) {
unset($value);
}
session_destroy();
print_r($_SESSION);
?>
I think that this script should work but it's not working for me. It gives me this output, without unsetting the variables :
我认为这个脚本应该可以工作,但它对我不起作用。它给了我这个输出,而没有取消设置变量:
Array ( [one] => one [two] => two [three] => three )
Array ( [one] => one [two] => two [three] => three )数组([1] => 1 [2] => 2 [3] => 3)数组([1] => 1 [2] => 2 [3] => 3)
Maybe it's a problem related to superglobal arrays. I can anyway unset the variables using their key :
也许这是与超全球阵列有关的问题。无论如何,我可以使用他们的密钥取消设置变量:
unset($_SESSION['one']);
3 个解决方案
#1
1
You must get the key and unset
$_SESSION[$key]
or pass your variable by reference (not sure if you can unset
a reference).
您必须获取密钥并取消设置$ _SESSION [$ key]或通过引用传递您的变量(不确定是否可以取消设置引用)。
foreach($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
foreach($_SESSION as &$value) {
unset($value);
}
#2
1
unset it with,
取消它,
unset($_SESSION);
#3
0
Pass the variable by reference :
通过引用传递变量:
foreach($_SESSION as &$value) {
// edit $value
}
#1
1
You must get the key and unset
$_SESSION[$key]
or pass your variable by reference (not sure if you can unset
a reference).
您必须获取密钥并取消设置$ _SESSION [$ key]或通过引用传递您的变量(不确定是否可以取消设置引用)。
foreach($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
foreach($_SESSION as &$value) {
unset($value);
}
#2
1
unset it with,
取消它,
unset($_SESSION);
#3
0
Pass the variable by reference :
通过引用传递变量:
foreach($_SESSION as &$value) {
// edit $value
}