deleteing blanks in an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

deleteing blanks in an array

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

combine http://php.net/array_filter with a callback that uses strlen() or any of the other functions to test a value...
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

i tried this but it never worked

$array1 = array_diff($array1, array(''));
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you even try what I posted about? :roll:
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

not sure entirely how to do what you are saying.... sorry.... i know i sound like i'm real thick!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
)
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. array_filter() returns a new array.
  2. your odd() doesn't return anything
  3. start posting code correctly, please. :evil:
traherom
Forum Newbie
Posts: 13
Joined: Tue Mar 15, 2005 6:43 am

Post 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.
Post Reply