How to trim a PHP array and remove all empty indexes
Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] => 4
[8] => 6
[9] =>
)
Output should be like
Array
(
[0] => 4
[1] => 6
)
You are looking for the
array_filterfunction 😉For instance, this portion of code :
Will give you the following output :
Note that all “falsy” values have been removed.
And if you want to be more specific, you can specify your own filtering function. For instance, to remove only
nulls from the array, I could use this :And I’d get :