Remove empty array items

From CodeCodex

PHP[edit]

<highlightsyntax>

 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;

}

</pre>