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? :roll:

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(&quote;a&quote;=>1, &quote;b&quote;=>2, &quote;c&quote;=>3, &quote;d&quote;=>4, &quote;e&quote;=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

echo &quote;Odd :\n&quote;;
print_r(array_filter($array1, &quote;odd&quote;));
echo &quote;Even:\n&quote;;
print_r(array_filter($array2, &quote;even&quote;));

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

and

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

and

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

and

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
  1. array_filter() returns a new array.
  2. your odd() doesn't return anything
  3. start posting code correctly, please. :evil:

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) {
  &#1111;0]=&gt;
  string(4) &quote;asdf&quote;
  &#1111;1]=&gt;
  string(5) &quote;sdfsd&quote;
  &#1111;2]=&gt;
  string(0) &quote;&quote;
  &#1111;3]=&gt;
  string(4) &quote;asdf&quote;
}

After:
array(3) {
  &#1111;0]=&gt;
  string(4) &quote;asdf&quote;
  &#1111;1]=&gt;
  string(5) &quote;sdfsd&quote;
  &#1111;3]=&gt;
  string(4) &quote;asdf&quote;
}
If you want to make the function I used through create_function into a full-blown, named function it'll work just as well.