Skip to content

PHP Remove Empty Or Whitespace Elements In Array

Here’s how you can remove empty or whitespace elements in your array, and also reindex the array at the same time. Use array_filter to remove the unwanted elements, and array_values to reindex the elements.

$arr = array_values( 
    array_filter($arr, function($item) { 
        return !empty(trim($item)); 
    }) 
); // remove empty or whitespace elements in array.

Quick and simple solution.

Enjoyed the content ? Share it with your friends !
Published inDevelopmentProgramming

Be First to Comment

Leave a Reply

Your email address will not be published.