deleteing blanks in an array
Moderator: General Moderators
deleteing blanks in an array
Hi all,
I have an array like this
array1(1,'',88,'','','',99,'')
i want to delete all the blanks in the array and reset the key in array1() so that it begins from 0.
How can i do this?
I have an array like this
array1(1,'',88,'','','',99,'')
i want to delete all the blanks in the array and reset the key in array1() so that it begins from 0.
How can i do this?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
combine http://php.net/array_filter with a callback that uses strlen() or any of the other functions to test a value...
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
have a read of their examples:
array_filter() wrote:Code: Select all
function odd($var) { return($var % 2 == 1); } function even($var) { return($var % 2 == 0); } $array1 = array("e;a"e;=>1, "e;b"e;=>2, "e;c"e;=>3, "e;d"e;=>4, "e;e"e;=>5); $array2 = array(6, 7, 8, 9, 10, 11, 12); echo "e;Odd :\n"e;; print_r(array_filter($array1, "e;odd"e;)); echo "e;Even:\n"e;; print_r(array_filter($array2, "e;even"e;));Code: Select all
Odd : Array ( їa] => 1 їc] => 3 їe] => 5 ) Even: Array ( ї0] => 6 ї2] => 8 ї4] => 10 ї6] => 12 )
i tried this and it does not work, in the callback how can i take out all blanks and create a new array out of it?
feyd | Please use
Code: Select all
function odd($var)
{
$var == '';
}
$narr = array();
$narr = array_filter($array1, "odd");
echo $narr[0];feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url]
the callback function must return something. Look carefully at their example callback functions again.i tried this and it output blank, so it never gets rid of the blanks
feyd | Please use
Code: Select all
<?php
function odd($var)
{
$var == '';
}
$narr = array();
array_filter($wfm_payment6,"odd");
echo $wfm_payment6[0];
if ($wfm_payment6[0] == '') { echo "blank"; }
?>feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] [b]*grumble-grumble*[/b][/color]Try this:
Output:
If you want to make the function I used through create_function into a full-blown, named function it'll work just as well.
Code: Select all
<?php
$your_array = array('asdf', 'sdfsd', '', 'asdf');
echo "Before:\n";
var_dump($your_array);
$your_array_without_blanks = array_filter($your_array,
create_function('$item', 'return !empty($item);'));
echo "\nAfter:\n";
var_dump($your_array_without_blanks);
?>Code: Select all
Before:
array(4) {
ї0]=>
string(4) "e;asdf"e;
ї1]=>
string(5) "e;sdfsd"e;
ї2]=>
string(0) "e;"e;
ї3]=>
string(4) "e;asdf"e;
}
After:
array(3) {
ї0]=>
string(4) "e;asdf"e;
ї1]=>
string(5) "e;sdfsd"e;
ї3]=>
string(4) "e;asdf"e;
}