1<?php
2$arr = array('1', '', '2', '3', '0');
3// Incorrect:
4print_r(array_filter($arr));
5// Correct:
6print_r(array_filter($arr, 'strlen'));
7//Custom
8print_r(array_filter($arr, function ($val) {if ($val > 0) {return true;} else {return false;}}));
1// One liner to remove empty ("" empty string) elements from your array.
2// Note: This code deliberately keeps null, 0 and false elements.
3$array = array_filter($array, function($a) {return $a !== "";});
4
5// OR if you want to trim your array elements first:
6// Note: This code also removes null and false elements.
7$array = array_filter($array, function($a) {
8 return trim($a) !== "";
9});
1$colors = array("red","","blue",NULL);
2
3$colorsNoEmptyOrNull = array_filter($colors, function($v){
4 return !is_null($v) && $v !== '';
5});
6//$colorsNoEmptyOrNull is now ["red","blue"]