Arrays\zip()

transformer lazy accepts iterable returns Closure pure
(U[], U?) → (Iterable → Iterable<[T, U]>)
At a glance — Pairs each source value with the value at the same index in the bound array. Lazy — yields pairs on demand.

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

Please note all arrays with keys will be converted to an indexed array/list.

/**
 * Returns a Closure for zipping together 2 arrays
 *
 * @param array $additional The Array to zip with
 * @param mixed $default The default value to use if the key does not exist in the additional array.
 * @return Closure(array<int|string, mixed>): array<int|string, mixed>
 */
Arrays\zip(array $additional, mixed $default = null): Closure

Returned Closure

When Arrays\zip() 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 a closure that zips together 2 arrays with 'FALLBACK' as the default value.
$zip = Arrays\zip(['a', 'b', 'c'], 'FALLBACK');

// Called as a function.  
var_dump($zip(['A', 'B']));   // [['a' , 'A'], ['b', 'B'], ['c', 'FALLBACK']]

Curried

This can be called inline using currying.

// Zip together 2 arrays with 'FALLBACK' as the default value.
var_dump(Arrays\zip(['a', 'b', 'c'], 'FALLBACK')(['A', 'B'])); 
// [['a' , 'A'], ['b', 'B'], ['c', 'FALLBACK']]

Details

Arrays Functions

Releated Array transformation Functions