information overload

by sebastian de deyne

Remove falsy values from a Laravel collection or array in PHP

13 Nov 2023

The native array_filter() in PHP and collect()->filter() in Laravel also work without providing a filter callback.

array_filter([0, 1, '', 'a', false, true, []]);
// [1, 'a', true]

collect([0, 1, '', 'a', false, true, []])->filter();
// [1, 'a', true]

If you don’t provide a callback, PHP will remove all empty values from the array.

Information Overload newsletter

I occasionally send out a dispatch with personal stories, things I'm working on, and interesting links I come across.

Only for occasional updates. No tracking.