I have two pages
the first just a form with two columns of text boxes. one for links and the other a discription of the link. This is all sent to the second page. this page generates a newsletter out of html from the links on the previous page. I'm grabing all the vars from the text boxes with $HTTP_POST_VARS. I then loop through this using url1, url2
works fine all the links show up in the news letter for emailing later.
<--- the problem ---->
all the boxes I left empty are blank and show up on my 2nd page under the filled in url's.. how can I take out the blank url's from $HTTP_POST_VARS or is there another way to do this?
thanks.
Removing blank keys in an array
Moderator: General Moderators
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
There is no a native PHP function to do this. You'll have to implement your own. Let me introduce you a technique:
output:
Hope it helps.
Have fun,
Scorphus.
Code: Select all
<?php
function supressEmpty ($array) {
$newArray = array();
foreach ($array as $k=> $v)
if (!empty($v))
$newArray[$k] = $v;
return $newArray;
}
$array = array(
'Name' => 'Scorphus',
'Occupation' => 'Developer',
'ProgLang' => 'PHP',
'DataBase' => 'MySQL',
'FirstName' => '', // empty
'LastName' => '', // empty
'Phone' => '8846-7255',
'email' => 'me_at_myserver.com'
);
print_r($array);
$array = supressEmpty($array);
print_r($array);
?>Code: Select all
Array
(
їName] => Scorphus
їOccupation] => Developer
їProgLang] => PHP
їDataBase] => MySQL
їFirstName] =>
їLastName] =>
їPhone] => 8846-7255
їemail] => me_at_myserver.com
)
Array
(
їName] => Scorphus
їOccupation] => Developer
їProgLang] => PHP
їDataBase] => MySQL
їPhone] => 8846-7255
їemail] => me_at_myserver.com
)Have fun,
Scorphus.
if you do not apply a callback function to array_filter() it filters out all elements that evaluate to false.The empty string/array, false and 0 are removed.
But as mentioned you can pass a function-name to array_filter that determines wether an element should be in or out. The return value is also casted to a boolean value, true->element's in, element's out otherwise
Code: Select all
<?php
$arr = array(
"a",
"",
"b",
false,
"c",
array(),
"d",
0
);
print_r($arr);
$arr = array_filter($arr);
print_r($arr);
?>But as mentioned you can pass a function-name to array_filter that determines wether an element should be in or out. The return value is also casted to a boolean value, true->element's in, element's out otherwise
Code: Select all
<?php
$arr = array(
"a",
"",
"b",
false,
"c",
0
);
print_r($arr);
$arr = array_filter($arr, "strlen");
print_r($arr);
?>