Removing empty array elements in PHP

You can use the array_filter function to remove empty elements from an array.   By default, this will any elements which evaluate to FALSE (null, false, '', 0).    It is possible to customize the filter by adding a callback function.

$array = [ 0, 'amir', 'test', 490, null, '', 'Hello world' ];
$array = array_filter($array);
Array
(
[1] => amir
[2] => test
[3] => 490
[6] => Hello world
)

This works fine but leaves index gaps in the array.  This can be fixed by using array_values to re-index the array.

$array = array_values($array);
Array
(
[0] => amir
[1] => test
[2] => 490
[3] => Hello world
)