I’ve seen this question for years, and it’s easy enough to write it out depending on your specific needs, but a simple way to remove empty array elements (“empty” meaning any element that evaluates to false), using build-in functions, while updating indexes, is:

array_values(array_filter($array));

array_filter – if passed with no callback, will automatically remove all elements that evaluate to FALSE… array_values will grab all those values and insert them into a new array, so indexes will be remapped.

POC:

$a = array(1, '', '', '', 2, '', 3, 4);
$b = array_values(array_filter($a));

print_r($b);