Remove empty items from a multidimensional array in PHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.