How to remove empty array elements with PHP?

Spread the love

Sometimes, we want to remove empty array elements with PHP.

In this article, we’ll look at how to remove empty array elements with PHP.

How to remove empty array elements with PHP?

To remove empty array elements with PHP, we use the array_filter method.

For instance, we write

print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));

to call array_filter with $linksArray and a function that returns

!is_null($value) && $value !== ''

to return an array that has the non null or empty string elements in $linksArray.

Conclusion

To remove empty array elements with PHP, we use the array_filter method.

Leave a Reply

Your email address will not be published.