Remove duplicate messages How to remove any duplicate messages of a certain type.

For example when performing 2 searches using the search api module before rendering the page the message "To perform a search you must enter at least 3 characters" may be displayed twice.
Use the following function to remove any duplicate messages of a certain type:

/**
 * @param $type - the type of messages that should be checked
 *
 * Removes any duplicate messages from the messages session
 */
function remove_duplicate_messages($type) {
  $stack = array();
  if(isset($_SESSION['messages'][$type])) {
    foreach($_SESSION['messages'][$type] as $k =&gt; $msg) {
      if(in_array($msg, $stack)) {
        // Message is already set - remove it
        unset($_SESSION['messages'][$type][$k]);
      } else {
        // Add message to stack
        $stack[] = $msg;
      }
    }
  }
}


Example use:

remove_duplicate_messages('warning');