I’ve run across a couple instances where I needed to either remove empty array elements or perform the equivalent of a trim() on PHP arrays. There are two pretty good ways to do this.
The first way involves building a custom function to loop through an array and unset empty elements like so:
function arrayRemoveEmpty($Array) {
foreach($Array AS $k => $v) {
if($Array[$k] == '') {
unset($Array[$k]);
}
}
return $Array;
}
The second is probably a more preferred, and certainly faster way. PHP has a built in function to handle filtering out empty elements, and it can be extended to do virtually anything you can imagine. Take a look:
$Array = array_filter($Array);