Posted By

jeffery2k2610 on 09/07/09


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
ignace
Mithun


Remove empty items from a multidimensional array in PHP


 / Published in: PHP
 

  1. <?
  2. function array_non_empty_items($input) {
  3. // If it is an element, then just return it
  4. if (!is_array($input)) {
  5. return $input;
  6. }
  7.  
  8. $non_empty_items = array();
  9.  
  10. foreach ($input as $key => $value) {
  11. // Ignore empty cells
  12. if($value) {
  13. // Use recursion to evaluate cells
  14. $non_empty_items[$key] = array_non_empty_items($value);
  15. }
  16. }
  17.  
  18. // Finally return the array without empty items
  19. return $non_empty_items;
  20. }
  21.  
  22. ?>

Report this snippet  

You need to login to post a comment.