How to remove null values from an array? [duplicate]

this will do the trick:

array_filter($arr, static function($var){return $var !== null;} );

Code Example: https://3v4l.org/jtQa2


for older versions (php<5.3):

function is_not_null ($var) { return !is_null($var); }
$filtered = array_filter($arr, 'is_not_null');

Code Example: http://3v4l.org/CKrYO

Leave a Comment