Arrays\replace()

transformer variadic terminal accepts iterable returns Closure pure
...T[] → (Iterable → T[])
At a glance — Wraps array_replace(). Terminal — Generator input is materialised first.

Creates a Closure that replaces values in an array or iterable based on matching keys — like array_replace() but curried.

/**
  * @param mixed[] ...$with Arrays whose values override the source at matching keys.
  * @return Closure(iterable<int|string, mixed>):mixed[]
  */
Arrays\replace(array ...$with): Closure

Returned Closure

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

/**
  * @param iterable<int|string, mixed> $source
  * @return mixed[]
  */
$function (iterable $source): array

Examples

Partial Application

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

$setXY = Arrays\replace(['x' => 1, 'y' => 2]);

print_r($setXY(['x' => 0, 'y' => 0, 'z' => 0]));
// ['x' => 1, 'y' => 2, 'z' => 0]

Details

Arrays Functions

Releated Array manipulation Functions