nandermind
0
Q:

php remove blank values from array

// PHP 7.4 and later
print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return !is_null($value) && $value !== ''; }));

// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));
2
// One liner to remove empty ("" empty string) elements from your array.
// Note: This code deliberately keeps null, 0 and false elements.
$array = array_filter($array, function($a) {return $a !== "";});

// OR if you want to trim your array elements first:
// Note: This code also removes null and false elements.
$array = array_filter($array, function($a) {
    return trim($a) !== "";
});
1
$colors = array("red","","blue",NULL);

$colorsNoEmptyOrNull = array_filter($colors, function($v){ 
 return !is_null($v) && $v !== ''; 
});
//$colorsNoEmptyOrNull is now ["red","blue"]
0

New to Communities?

Join the community