Re: [PHP] How to get rid off empty elements of an array?
2006/8/7, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: 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 ''; } 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).
Re: [PHP] How to get rid off empty elements of an array?
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, [EMAIL PROTECTED] <[EMAIL PROTECTED]> 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 ''; } 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] SOLVED (spelling error) - Re: [PHP] How to get rid off empty elements of an array?
Sorry for this. Spelling error. :( -afan > 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 ' 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 > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] How to get rid off empty elements of an array?
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 ''; } 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