Thinking in programming

Things I like to talk about programming

Remove empty items from a multidimensional array in PHP

with 7 comments

Removing empty items from a single-level array is something trivial in PHP; but it is not so if our array has 2 levels or more. The following function removes empty items from an (multidimensional) array, and returns the resulting array. Please notice that the use of recursion is required because of the array can be of an unknown number of levels, that is, arrays inside the cells of the array, an so on.

This function is useful when by instance, we have a search-form and the user can enter some optional criteria.

  function array_non_empty_items($input) {
    // If it is an element, then just return it
    if (!is_array($input)) {
      return $input;
    }

    $non_empty_items = array();

    foreach ($input as $key => $value) {
      // Ignore empty cells
      if($value) {
        // Use recursion to evaluate cells
        $non_empty_items[$key] = array_non_empty_items($value);
      }
    }

    // Finally return the array without empty items
    return $non_empty_items;
  }

By example, if we pass the following array to the function:

    $anArray = array(
      ‘country’ => ‘CO’,
      ‘region’ => ,
      ‘city’ => ,
      ‘features’ => array(
          ‘home’ => array(‘garden’, ‘bedrooms’=>3),
          ‘beach’ => array(‘no_more_than’=>30, ‘yachts_rental’),
          ‘supermarkets’ => ,
          ‘discotheque’ =>
       )
    );

Then we will obtain an array without empty items, like this:

   $anArray = array(
      ‘country’ => ‘CO’,
      ‘features’ => array(
          ‘home’ => array(‘garden’, ‘bedrooms’=>3),
          ‘beach’ => array(‘no_more_than’=>30, ‘yachts_rental’)
       )
    );
Advertisements

Written by roger.padilla

August 21, 2009 at 18:43

Posted in PHP

Tagged with , ,

7 Responses

Subscribe to comments with RSS.

  1. Great work guys this worked well for me

    Thanks for this good code..

    jeffery

    August 29, 2009 at 05:42

  2. I think…….!
    You need to update with some new stuffs….!

    Jesus Villalobos

    May 11, 2010 at 20:26

  3. Mmm maybe u can check the native function array_filter 😉 Easy!

    http://php.net/manual/en/function.array-filter.php

    Regards.

    Carxl

    October 11, 2010 at 07:03

  4. Hi Andres!

    array_filter does not traverse along all the dimensions of a multidimensional-array, it was designed for arrays of a single dimension; the function I’ve created will help on such cases.

    roger.padilla

    October 11, 2010 at 10:46

  5. Not sure if you still check this, it’s a pretty old post, but I can’t seem to get it to work. Here’s a var_dump of my array:

    array (size=8)
    0 =>
    array (size=1)
    0 => int 1
    1 =>
    array (size=1)
    1 => int 2
    2 =>
    array (size=1)
    10 => int 1
    3 =>
    array (size=1)
    11 => int 2
    4 =>
    array (size=1)
    15 => int 1
    5 =>
    array (size=1)
    16 => int 1
    6 =>
    array (size=1)
    17 => int 1
    7 =>
    array (size=1)
    22 => int 1

    After running th function, the array is not affected. Any ideas?

    Ifor Williams

    September 17, 2014 at 09:00

    • Hi Ifor,

      The original array is not modified; the function returns a new array, therefore you need to store the result of calling the function.

      // after the following line, the $filteredArr variable will contain the array without empty items.
      $filteredArr = array_non_empty_items($originalArr);

      roger.padilla

      September 17, 2014 at 21:19


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: