public static function filterRecursive(array $array, $callback = null, $removeEmptyArrays = false)
    {
        if (null !== $callback && !is_callable($callback)) {
            throw new Exception\InvalidArgumentException(sprintf(
                'Second parameter of %s must be callable',
                __METHOD__
            ));
        }

        foreach ($array as $key => &$value) { // mind the reference
            if (is_array($value)) {
                $value = static::filterRecursive($value, $callback, $removeEmptyArrays);

                if ($removeEmptyArrays && ! (bool) $value) {
                    unset($array[$key]);
                }
            } else {
                if (null !== $callback && !$callback($value)) {
                    unset($array[$key]);
                } elseif (! (bool) $value) {
                    unset($array[$key]);
                }
            }
        }

        unset($value); // kill the reference

        return $array;
    }