note 17122 added to ref.array
| From: | janfolkert at movenext dot nl | Date: | Tue, 27 Nov 2001 09:13:22 +0000 |
| Subject: | note 17122 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-21316@lists.php.net to get a copy of this message | ||
I came across quite a few empty values
in my arrays, which kind of annoyed me a
bit, as i really couldn't use that kind
of thing.
I made this little snippet, that removes
all arrays from an array, and also all
empty values in that array, and returns
what it cleared.
If you don't want the sub-arrays to be
removed, change the next line:
$retval = array_merge($retval, $value);
to:
$retval[] = $value);
It's recursive, so it doesn't really care
how many sub-arrays you have, just
as long as you have enough memory.
function remove_empty($array)
{
foreach($array as $key=>$value)
{
if(is_array($value))
{
$value = $this->remove_empty($value);
$retval = array_merge($retval, $value);
}
else
{
if($value != "")
$retval[] = $value;
}
}
foreach($retval as $key=>$value)
{
if($value != "")
$ret[] = $value;
}
return $ret;
}
(this note was posted as a thanks to all
the other people who helped by giving
advice and help on here.)
--
http://www.php.net/manual/en/ref.array.php
http://master.php.net/manage/user-notes.php?action=edit+17122
http://master.php.net/manage/user-notes.php?action=delete+17122
http://master.php.net/manage/user-notes.php?action=reject+17122