Quantcast
Channel: Remove empty array elements - Stack Overflow
Viewing all articles
Browse latest Browse all 28

Answer by Chrisdigital for Remove empty array elements

$
0
0

Just want to contribute an alternative to loops...also addressing gaps in keys...

In my case I wanted to keep sequential array keys when the operation was complete (not just odd numbers, which is what I was staring at. Setting up code to look just for odd keys seemed fragile to me and not future-friendly.)

I was looking for something more like this: http://gotofritz.net/blog/howto/removing-empty-array-elements-php/

The combination of array_filter and array_slice does the trick.

$example = array_filter($example); $example = array_slice($example,0);

No idea on efficiencies or benchmarks but it works.


Viewing all articles
Browse latest Browse all 28

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>