Array remove empty entries
Removes empty entries from a array recursivly
function array_remove_empty($arr){ $narr = array(); while(list($key, $val) = each($arr)){ if (is_array($val)){ $val = array_remove_empty($val); // does the result array contain anything? if (count($val)!=0){ // yes :-) $narr[$key] = $val; } } else { if (trim($val) != ""){ $narr[$key] = $val; } } } unset($arr); return $narr; }
Author:
Jonas John
License:
Public Domain
Language:
PHP
Created:
03/28/2006
Updated:
03/28/2006
Tags:
array functions, cleanup
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
array_filter($the_array);
ta da!
$a = array('a','','c');
$a = array_diff($a, array(''))
// $a will be array('a','c');