2013-09-11 19 views
0

如何刪除刪除會議,未設置會話變量開始與主圖案像與關鍵起點爲特定的字符

哪裏像

Guard_1660743344 
Guard_4323340344 
Guard_5343332233 
..... 
Guard_[dynamicvalue] 

鍵我想從紙盒中取出所有會話變量如果會話密鑰開始與「Guard_」

unset($_SESSION[$key]); 

對不起,我盡了最大努力找到解決方法,但失敗了,因此要求

回答

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 

?> 

請注意,循環上​​將比循環整個$_SESSION陣列更有效..

+1

@Boaz你是對的,但是爲了提高效率,我改變了它以循環'array_keys($ _ SESSION)'..所以在這種情況下'作爲$鑰匙'是好的:) –

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 
    } 
}