Arrays\toString()

reducer terminal accepts iterable returns Closure pure
string? → (Iterable → string)
At a glance — Reduces an iterable to a single string. Terminal — a Generator source is fully materialised before joining.

Creates a function which casts an array to a string using the defined glue.

/**
 * Returns a Closure for casting an array to a string.
 *
 * @param string|null $glue
 * @return Closure(array<int|string, mixed>): string
 */
Arrays\toString(?string $glue): Closure

Returned Closure

When Arrays\toString() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param array<int|string, mixed> $array
  * @return string
  */
$function (array $data): string

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

// Create the closure that casts an array to a string with a comma as the glue.
$commaSeparated = Arrays\toString(',');

// Called as a function.
var_dump($commaSeparated(['b', 'c'])); // b,c

Curried

This can be called inline using currying.

// Casts to a string with - as the glue

 print Arrays\toString('-')(['b', 'c']); // b-c

Inlined with Higher Order Function

If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.

$array = array_map( Arrays\toString('---'), [
   ['Once', 'upon', 'time'],
   ['Find', 'the', 'time']
]);

print_r($array); // ['Once---upon---time', 'Find---the---time']

Details

Arrays Functions

Releated Array transformation Functions