Page 1 of 1

Removing empty elements from an array

Posted: Mon Apr 19, 2004 6:33 am
by kettle_drum
ANybody know how i can remove empty elements from an array?

I.e i have:

Code: Select all

Array ( ї0] => )
and count() says there is one item in the array - but its empty.

Posted: Mon Apr 19, 2004 6:37 am
by JayBird
something like this

Code: Select all

foreach($array as $key => $value) { 
  if($value == "") { 
    unset($array[$key]); 
  } 
} 
$new_array = array_values($array);
???

Mark

Posted: Mon Apr 19, 2004 6:55 am
by CoderGoblin
As far as I know there is not a remove empty array function so you have to step through the array. Two possibilities spring to mind.

1) use the command array_filter
2) use a foreach/while with array_shift and build a new array.

Out of preference I would use option 1.

Regards