Re: Removing empty array values.

From: Date: Mon, 18 Aug 2003 12:26:40 +0000
Subject: Re: Removing empty array values.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-159908@lists.php.net to get a copy of this message
On Mon, 18 Aug 2003 08:03:25 -0400, you wrote: >How do I remove empty array values? That could mean a lot of things. I'm going to assume you have a simple indexed array and want to remove any entries where array[index] == FALSE, and reindex the array. The most elegant way is to copy all the values you want to keep to a new array. $a = array(FALSE, 2, 3, FALSE, 5, FALSE, 7, FALSE, FALSE, FALSE); $b = array(); for ($i = 0; $i < sizeof ($a); $i++) { if ($a[$i] !== FALSE) { $b[] = $a[$i]; } } $a = $b; print_r ($a); If you don't want to reindex the array, you could use unset() $a = array(FALSE, 2, 3, FALSE, 5, FALSE, 7, FALSE, FALSE, FALSE); for ($i = 0; $i < sizeof ($a); $i++) { if ($a[$i] === FALSE) { unset ($a[$i]); } } print_r ($a);

« previous php.general (#159908) next »