Unset all session variables with similar name

后端 未结 4 811
生来不讨喜
生来不讨喜 2021-01-24 08:55

I\'m using some $_SESSION variables for filtering many query records that have a similar name (ex. $_SESSION[\'nameFilter\'] or $_SESSION[\'cityF

4条回答
  •  花落未央
    2021-01-24 09:39

    Use foreach to enumerate the keys of $_SESSION[], use substr() to get the last 6 characters of each key, use unset() to (what else?) unset it.

    As easy as:

    session_start();
    foreach (array_keys($_SESSION) as $key) {
        if (substr($key, -6) == 'Filter') {
            unset($_SESSION[$key]);
        }
    }
    

提交回复
热议问题