Remove Empty Array Elements and reindex in PHP

The built-in array_filter() the function removes all the empty elements, zeros, false and null values from an array. This function uses a callback function to filter the array values. If no callback function is specified, it removes the empty elements, zeros, false and null values.


$array is an array to iterate over,
$callback is a callback function if no callback is supplied, all entries of the array equal to FALSE will be removed.
$flag determining what arguments are sent to the callback, default is zero, it means only array values will be passed to the callback function. Following are the two flangs that we can specify
ARRAY_FILTER_USE_KEY – passkey as the only argument to callback instead of the value
ARRAY_FILTER_USE_BOTH – pass both value and key as arguments to callback instead of the value

array_filter() function Example
array_filter() function Example with callback

You can remove the empty value and then reindex array elements too easily, below is the example

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments Protection Status
Would love your thoughts, please comment.x