Re: How to get rid off empty elements of an array?

From: Date: Mon, 07 Aug 2006 16:51:13 +0000
Subject: Re: How to get rid off empty elements of an array?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-240267@lists.php.net to get a copy of this message
2006/8/7, afan@afan.net <afan@afan.net>:
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php There are probably some whitespaces in the elements which refuse to be
deleted. Also consider using array_filter() instead of looping through the array (used without a callback function filter all elements which evaluate to boolean false).

« previous php.general (#240267) next »