How to remove empty values from an array in PHP

We mostly work in array to pass the value in the application. And many times we encountered the null value in an array which we have to remove as per requirement and flow in the application, and for that, we use foreach loop to remove that empty value. You can perform this activity without for each loop in PHP.

PHP has an array_filter function by which you can remove all empty values, from an array. In this post, we will explain to you how you can remove empty values from an array in PHP using array_filter function.

$example = array(200, "Vishnu", "","Hi", "", "By");

$resultArray = array_filter($example);
print_r($resultArray);

Output

Array
(
    [0] => 200
    [1] => Vishnu
    [3] => Hi
    [5] => By
)

Spread the love

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *