For the data you have provided in your question, use the array_filter()
function with an empty callback parameter. This will filter out all empty elements.
$array = array( ... );
$array = array_filter($array);
If you need to filter the elements you described in your question text, then you need to add a callback function that will return true
(valid) or false
(invalid) depending on what you need. You might find the ctype_alpha
functions useful for that.
$array = array( ... );
$array = array_filter($array, 'ctype_alpha');
If you need to allow dashes as well, you need to provide an own function as callback:
$array = array( ... );
$array = array_filter($array, function($test) {return preg_match('(^[a-zA-Z-]+$)', $test);});
This sample callback function is making use of the preg_match()
function using a regular expression. Regular expressions can be formulated to represent a specifc group of characters, like here a-z
, A-Z
and the dash -
(minus sign) in the example.