Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active September 19, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eusonlito/c358fca7e9b62c40bb42707a12d340b7 to your computer and use it in GitHub Desktop.
Save eusonlito/c358fca7e9b62c40bb42707a12d340b7 to your computer and use it in GitHub Desktop.
PHP array_filter_recursive helper
<?php declare(strict_types=1);
if (!function_exists('array_filter_recursive')) {
/**
* @param array $array
* @param ?callable $callback = null
*
* @return array
*/
function array_filter_recursive(array $array, ?callable $callback = null): array
{
if ($callback === null) {
$callback = static function ($value) {
return $value;
};
}
return array_filter(array_map(static function ($value) use ($callback) {
return is_array($value) ? array_filter_recursive($value, $callback) : $value;
}, $array), $callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment