Re: Removing empty values from array

From: Date: Thu, 25 Jun 2009 21:00:07 +0000
Subject: Re: Removing empty values from array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-294616@lists.php.net to get a copy of this message
Martin Scotta wrote:
Hi all I have this in a simple routine... for($i=0, $if=count($array); $i<$if; ++$i) if( $array[$i] == '' ) { array_splice( $array, $i, 1); --$i; --$if; } My question: is this the better way to do it?
<?php foreach( array_keys( $array ) as $key ) {
    if( (string)$array[$key] === '' )
    {
        unset( $array[$key] );
    }
} ?> The above will not remove a value of '0' which IMHO is not empty. If you actually want to get rid of '0' also, then change === to == and remove the (string) cast. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#294616) next »