Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active November 11, 2015 20:35
Show Gist options
  • Save kjbrum/d701c2cf538e3dc60a5f to your computer and use it in GitHub Desktop.
Save kjbrum/d701c2cf538e3dc60a5f to your computer and use it in GitHub Desktop.
Remove empty values from an array.
<?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