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
kettle_drum
DevNet Resident
Posts: 1150 Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England
Post
by kettle_drum » Mon Apr 19, 2004 6:33 am
ANybody know how i can remove empty elements from an array?
I.e i have:
and count() says there is one item in the array - but its empty.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Mon Apr 19, 2004 6:37 am
something like this
Code: Select all
foreach($array as $key => $value) {
if($value == "") {
unset($array[$key]);
}
}
$new_array = array_values($array);
???
Mark
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Mon Apr 19, 2004 6:55 am
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