Page 1 of 1
deleteing blanks in an array
Posted: Wed Mar 16, 2005 10:10 am
by gurjit
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?
Posted: Wed Mar 16, 2005 10:18 am
by feyd
combine
http://php.net/array_filter with a callback that uses strlen() or any of the other functions to test a value...
Posted: Wed Mar 16, 2005 11:12 am
by gurjit
i tried this but it never worked
$array1 = array_diff($array1, array(''));
Posted: Wed Mar 16, 2005 11:24 am
by feyd
did you even try what I posted about?

Posted: Wed Mar 16, 2005 11:32 am
by gurjit
not sure entirely how to do what you are saying.... sorry.... i know i sound like i'm real thick!!!
Posted: Wed Mar 16, 2005 11:39 am
by feyd
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
)
Posted: Wed Mar 16, 2005 1:46 pm
by gurjit
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?
Code: Select all
function odd($var)
{
$var == '';
}
$narr = array();
$narr = array_filter($array1, "odd");
echo $narr[0];
feyd | Please use Code: 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]
Posted: Wed Mar 16, 2005 1:59 pm
by feyd
Please use
Code: 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.
Posted: Wed Mar 16, 2005 2:22 pm
by gurjit
i tried this and it output blank, so it never gets rid of the blanks
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
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]
Posted: Wed Mar 16, 2005 2:32 pm
by feyd
- array_filter() returns a new array.
- your odd() doesn't return anything
- start posting code correctly, please.

Posted: Wed Mar 16, 2005 4:40 pm
by traherom
Try this:
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);
?>
Output:
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;
}
If you want to make the function I used through create_function into a full-blown, named function it'll work just as well.