Skip to content

Instantly share code, notes, and snippets.

@farinspace
Created July 6, 2011 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farinspace/1068596 to your computer and use it in GitHub Desktop.
Save farinspace/1068596 to your computer and use it in GitHub Desktop.
PHP function to clean an array recursively
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