Created
July 6, 2011 23:35
-
-
Save farinspace/1068596 to your computer and use it in GitHub Desktop.
PHP function to clean an array recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function clean(&$arr) | |
{ | |
if (is_array($arr)) | |
{ | |
foreach ($arr as $i => $v) | |
{ | |
if (is_array($arr[$i])) | |
{ | |
clean($arr[$i]); | |
if (!count($arr[$i])) | |
{ | |
unset($arr[$i]); | |
} | |
} | |
else | |
{ | |
if ('' == trim($arr[$i]) OR is_null($arr[$i])) | |
{ | |
unset($arr[$i]); | |
} | |
} | |
} | |
if (!count($arr)) | |
{ | |
$arr = array(); | |
} | |
else | |
{ | |
$keys = array_keys($arr); | |
$is_numeric = TRUE; | |
foreach ($keys as $key) | |
{ | |
if (!is_numeric($key)) | |
{ | |
$is_numeric = FALSE; | |
break; | |
} | |
} | |
if ($is_numeric) | |
{ | |
$arr = array_values($arr); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment