Remove NULL, FALSE, and ” – but not 0 – from a PHP array

array_filter should work fine if you use the identical comparison operator.

here’s an example

$values = [NULL, FALSE, '', 0, 1];

function myFilter($var){
  return ($var !== NULL && $var !== FALSE && $var !== '');
}

$res = array_filter($values, 'myFilter');

Or if you don’t want to define a filtering function, you can also use an anonymous function (closure):

$res = array_filter($values, function($value) {
    return ($value !== null && $value !== false && $value !== ''); 
});

If you just need the numeric values you can use is_numeric as your callback: example

$res = array_filter($values, 'is_numeric');

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)