How to get rid off empty elements of an array?

From: Date: Mon, 07 Aug 2006 14:18:20 +0000
Subject: How to get rid off empty elements of an array?
Groups: php.general 
Request: Send a blank email to php-general+get-240253@lists.php.net to get a copy of this message
Hi to all! I have a form with 6 text fields where visitor has to enter product serial number (same type data). he has to enter AT LEAST ONE. for ($i=0; $i<6; $i++) { echo '<input type="text" name="PSN['.$i.']" value="'.$PSN[$i].'" size="25">'; } After the form is submitted I'm getting an array Array ( [0] => abc123 [1] => ddd111 [2] => poi987 [3] => [4] => [5] => ) Now, I need to get rid off empty elements of the array, to get this: Array ( [0] => abc123 [1] => ddd111 [2] => poi987 ) To do that I tried: foreach ($PSN as $value) { if (!empty($value)) $PSN_New[] = $value; } but every time I'm getting one additional element of the array: I tried foreach ($PSN as $value) { if ($value != '') $PSN_New[] = $value; } too - same result. Array ( [0] => abc123 [1] => ddd111 [2] => poi987 [3] => ) No space or anything else is "entered" in 4th field. What I'm doing wrong? Thanks for any help. -afan

« previous php.general (#240253) next »