Remove empty array elements in PHP

You can use the PHP array_filter() function remove empty array elements or values from an array in PHP. This will also remove blank, null, false, 0 (zero) values.

Filters elements of an array using a callback function. if no callback is supplied, all entries of array equal to FALSE will be removed.

Remove Empty Array Elements In PHP

$arr = array("PHP", "HTML", "CSS", "", "JavaScript", null, 0);
print_r(array_filter($arr)); // removing blank, null, false, 0 (zero) values


    [0] => PHP
    [1] => HTML
    [2] => CSS
    [4] => JavaScript

Leave A Reply

Your email address will not be published.