PHP Cleaning Arrays…

When arrays are built by user input. You can sometimes experience “blank” values within that array. Sometimes set to “0”, “FALSE” or “NULL”.

You can use array_filter to remove empty (NULL) elements:

$emptyRemoved = array_filter($linksArray);

If you have (int) 0 in your array, you may use the following:

$emptyRemoved = remove_empty($linksArray);

function remove_empty($array) {
 return array_filter($array, '_remove_empty_internal');
 }

function _remove_empty_internal($value) {
 return !empty($value) || $value === 0;
 }

Maybe your elements are not empty per say but contain one or more spaces… You can use the following before using array_filter

$trimmedArray = array_map('trim', $linksArray);

 

Sourced from: http://stackoverflow.com/questions/3654295/remove-empty-array-elements

PHP Cleaning Arrays…

Leave a Reply

Your email address will not be published. Required fields are marked *