Unset all session variables with similar name

后端 未结 4 813
生来不讨喜
生来不讨喜 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:41

    Assuming your keys always cointain the string Filter you can check for it.

    I suggest you to take a look at the strpos function which checks if a given needle is cointaned in a string and returns the null in case it's not found or the position of where the needle starts in that string.

    Then you only have to go through the session variables and unset the ones containing the word Filter

    foreach($_SESSION as $key => $value){
      if (strpos($key, 'Filter') !== false) {
        unset($_SESSION[$key]);
      }
    }
    

    Hope this helps :)

提交回复
热议问题