如何从
等关键模式开始删除,取消设置会话变量像
这样的键Guard_1660743344
Guard_4323340344
Guard_5343332233
.....
Guard_[dynamicvalue]
如果会话密钥以“Guard _”
开头,我想从托盘中删除所有会话变量unset($_SESSION[$key]);
抱歉,我尽力找到解决办法但失败了,因此问了
答案 0 :(得分:6)
我想在这种情况下foreach
循环是最好的:( Working eval.in )
<?php
foreach(array_keys($_SESSION) as $key) // loop over all keys of the session
if(substr($key,0,6)=='Guard_') // if the key starts with Guard_
unset($_SESSION[$key]); // unset it
?>
请注意,在array_keys($_SESSION)
上循环比循环整个$_SESSION
数组更有效..
答案 1 :(得分:0)
这很简单:
foreach($_SESSION as $key => $value) { //iterate over all session keys
if(substr($key, 0, 6) === 'Guard_') { //test if the key prefix matches
unset($_SESSION[$key]); //if it does, remove it from the array
}
}