note 17122 deleted from ref.array by jimw

From: Date: Sat, 15 Dec 2001 22:34:23 +0000
Subject: note 17122 deleted from ref.array by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-22915@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.)

« previous php.notes (#22915) next »