The Code Developer

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.

array_filter
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

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

Output:

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