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

From: Date: Mon, 07 Aug 2006 15:38:03 +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-240265@lists.php.net to get a copy of this message
Check the 5th and 6th fields. Something is in one of them. Because based on your checks, it will work: [php] $first_array = array( 'first', 'second', 'third', '', '', ''); foreach ($first_array as $value) {
if (!empty($value))    $second_array[] = $value;
} var_dump($second_array); foreach ($first_array as $value) {
if ($value != '')    $third_array[] = $value;
} var_dump($third_array); [/php] HTH, John W On 8/7/06, afan@afan.net <afan@afan.net> wrote:
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


« previous php.general (#240265) next »