Quantcast
Channel: Remove empty array elements - Stack Overflow
Viewing all articles
Browse latest Browse all 28

Answer by marcovtwout for Remove empty array elements

$
0
0

Another one liner to remove empty ("" empty string) elements from your array.

$array = array_filter($array, function($a) {return $a !== "";});

Note: This code deliberately keeps null, 0 and false elements.


Or maybe you want to trim your array elements first:

$array = array_filter($array, function($a) {
    return trim($a) !== "";
});

Note: This code also removes null and false elements.


Viewing all articles
Browse latest Browse all 28

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>