Skip to content

Instantly share code, notes, and snippets.

@slav123
Created October 13, 2014 02:44
Show Gist options
  • Save slav123/db0a385b7f4d965c528b to your computer and use it in GitHub Desktop.
Save slav123/db0a385b7f4d965c528b to your computer and use it in GitHub Desktop.
remove null or false from table
<?php
/**
I had an array with something like the following: Array ( [0] => [1] => test [2] => fun ). But I don't want [0], the empty value in the array.
After searching the web for a good solution, I saw that people were using anywhere from 4 to 10+ lines of code to remove null values from arrays. This is obviously overkill so I decided to tackle the problem myself.
Remove NULL values only
**/
$new_array_without_nulls = array_filter($array_with_nulls, 'strlen');
//Remove any FALSE values
//This includes NULL values, EMPTY arrays, etc. Thanks to Paul Scott for pointing out this method.
$new_array_without_nulls = array_filter($array_with_nulls);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment