Last active
November 11, 2015 20:35
-
-
Save kjbrum/d701c2cf538e3dc60a5f to your computer and use it in GitHub Desktop.
Remove empty values from an array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove empty values from an array. | |
* | |
* @param array $arr The array to remove values from | |
* @return array The new array without the empty values | |
*/ | |
function remove_empty_values( $arr ) { | |
if( ! is_array( $arr ) ) return $arr; | |
$non_empty_items = array(); | |
foreach( $arr as $key => $value ) { | |
if( $value && ! empty( $value ) ) { | |
$non_empty_items[$key] = remove_empty_values( $value ); | |
} | |
} | |
return array_filter( $non_empty_items ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment