removing empty elements from an array, the php way :)

33 thoughts on “removing empty elements from an array, the php way :)”

  1. hasin vai, i know that is a copy paste mistake 😉 another thing at last code example on line 3
    i think replacing this line
    $empty_elements = array_keys($array,””);
    with this
    $empty_elements = array_keys($array,”);
    will improve performace a little 😉

  2. That will not help, because you set the empty elements randomly.
    There could be the (extreme) case, that only one element is empty, because mt_rand could produce always the same number in 1000 runs.
    You should generate the array once and duplicate it for each test…

  3. @anonymous, that’s left as an exercise for the reader: write one bit of code that calls generateRandomArray() ONCE; use something like to create new, pristine copies of that array which can then be run against the individual benchmarks. That way, you’ll be guaranteed that each benchmark is running against the same data. Remember to turn off your caching first…

  4. After deleting cache and using the same array for a machine of 1GB RAM and DUAL CORE 2.20 GHz each

    1. N times loop : 0.00297999382019
    2. Using Array diff : 0.108612060547
    3. Using Array filter : 0.0038890838623
    4. Using Array Keys : 0.000926971435547

  5. @Jórg, @Dynom – thanks for your comments. the code was initially written te remove both ” ” (thats a blank space,  ) and an empty element where array_filter doesnt work without a callback. and for the array with 100000 elements or bigger, yeah the time does matter.

    i’ve written that “most of the time such microseconds” doesn’t matter, but hey – optimization is the all about microseconds, eh?


    Thanks for your comments 🙂

  6. I really don’t understand how you can compare the times. I get that you would like the user to go off and do their own benchmarking so these tests run on the same data set but then, what is the reason for the microsecond timers at all?

    Given that you are referring to the methods as “improvements”, you are misleading readers.


    1. (Micro-)Optimizing for performance may not be important for one-off projects and/or low-traffic websites, but when you are maintaining a framework that is used by many different websites, and certain code runs on every single page request, then yes, optimizations are worth investing time into. Microseconds do add up over the course of a day once you factor in hundreds of thousands of runs.

  7. I think you need to take more attention to manual section about array, as you have been noticed about array_filter you can use.


    so we can just do:
    $array = generateRandomArray();
    $start= microtime(true);
    $array = array_map(’empty’, $array);
    $end = microtime(true);
    echo ($end-$start);

    or use custom function to walk/map array, also you are free to use strlen or is_null as parameter for function name. Don’t forget that you can pass parameters to walk/map

  8. If you dont need to be selective on the key, for example if you only want to remove the first or last element, you could also use the native php functions: array_shift() and array_pop() – I have a feeling these would provide faster results.

  9. Oh by the way, I found this very helpful also:
    function array_filter_recursive($input)
    foreach ($input as &$value) {
    if (is_array($value)) {
    $value = array_filter_recursive($value);
    return array_filter($input);

    This was from a user comment on

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s