Creates a Closure that flattens nested arrays up to a specified depth. Empty nested arrays are dropped.
/**
* @param int|null $n Depth to flatten. Null = fully.
* @return Closure(iterable<int|string, mixed>):(array<int, mixed>|\Generator<int, mixed>)
*/
Arrays\flattenByN(?int $n = null): ClosureWhen Arrays\flattenByN() is called, it returns the following Closure which can be used like a regular function.
/**
* @param iterable<int|string, mixed> $source
* @return array<int, mixed>|\Generator<int, mixed>
*/
$function (iterable $source): array|\GeneratorThis can be used to create a simple closure which can be used as a regular function.
$flatOne = Arrays\flattenByN(1);
print_r($flatOne([1, [2, [3, 4]], 5]));
// [1, 2, [3, 4], 5]
$flatAll = Arrays\flattenByN();
print_r($flatAll([1, [2, [3, 4]], 5]));
// [1, 2, 3, 4, 5]This can be called inline using currying.
print_r(Arrays\flattenByN(2)([[[1]], [[2], [3]]]));
// [1, [2], [3]] // wait — 2 levels deep