-
-
Save theperfectwill/aa2bd28e2de4cb308738fff029451cdf to your computer and use it in GitHub Desktop.
Remove empty keys from an array recursively.
This file contains 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 keys from an array recursively. | |
* | |
* @param array $array The array to be checked. | |
* @param boolean $make_empty If the output is to return as an empty string. | |
* @since 1.29 | |
* @see http://stackoverflow.com/questions/7696548/php-how-to-remove-empty-entries-of-an-array-recursively | |
*/ | |
function pis_array_remove_empty_keys( $array, $make_empty = false ) { | |
if ( ! is_array( $array ) ) { | |
return; | |
} | |
foreach ( $array as $key => $value ) { | |
if ( is_array( $value ) ) { | |
$array[$key] = pis_array_remove_empty_keys( $array[$key] ); | |
} | |
if ( empty( $array[$key] ) ) { | |
unset( $array[$key] ); | |
} | |
} | |
if ( empty( $array ) && $make_empty ) { | |
$array = ''; | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment