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.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.