src/Arr.php
private static function doFlatten($iterable, $depth, callable $predicate, array $result = [])
    {
        foreach ($iterable as $item) {
            if ($depth >= 1 && $predicate($item)) {
                $result = static::doFlatten($item, $depth - 1, $predicate, $result);
            } else {
                $result[] = $item;
            }
        }

        return $result;
    }
private static function doFlatten($iterable, $depth, callable $predicate, array $result = [])
    {
        foreach ($iterable as $item) {
            if ($depth >= 1 && $predicate($item)) {
                $result = static::doFlatten($item, $depth - 1, $predicate, $result);
            } else {
                $result[] = $item;
            }
        }

        return $result;
    }
Internal method to do actual flatten recursion after args have been validated by main method.

@param iterable $iterable  The iterable to flatten
@param int      $depth     How deep to flatten
@param callable $predicate Whether to recurse the item
@param array    $result    The result array

@return array