Re: unset empty elements in an array

From: Date: Mon, 12 Jul 2004 10:04:09 +0000
Subject: Re: unset empty elements in an array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-190231@lists.php.net to get a copy of this message
Daniel Kullik wrote:
Justin French wrote:
Hi, Looking for a one-liner to delete all empty elements in an array. I know I can do it with a foreach loop, but I'm hoping that I've missed an existing function in the manual which may already do this, or a simple one-liner to replace the foreach. <?php foreach($in as $k => $v) {
    if(empty($v)) {
        unset($in[$k]);
    }
} ?> --- Justin French http://indent.com.au
Though it's not really a one-liner: [code] while ($key = array_search('', $in)) unset($in[$key]); [/code] For more infos on array_seach(): http://www.php.net/array_search Daniel
A note I forgot to addi in my previous posting: You ought to think over what you consider "empty" since empty() would for example return true if the checked variable contained the integer 0 or the string '0'. My posted line of code recognizes only an empty string as "empty". You might want to take a look at this: http://www.php.net/manual/en/types.comparisons.php Daniel -- WWE e-commerce IT GmbH Eiffestrasse 462, D-20537 Hamburg Tel.: +49-40-2530659-0, Fax: +49-40-2530659-50

« previous php.general (#190231) next »