Arrays\toJson()

transformer terminal accepts iterable returns Closure pure
(int, int) → (mixed → string | null)
At a glance — Wraps json_encode(). Bind flags once (e.g. JSON_PRETTY_PRINT) for consistent output.

Creates a Closure that JSON-encodes an array or iterable with pre-bound flags and depth.

/**
  * @param int $flags json_encode flags.
  * @param int $depth Nodes deep to encode.
  * @return Closure(mixed):?string
  */
Arrays\toJson(int $flags = 0, int $depth = 512): Closure

Returned Closure

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

/**
  * @param mixed $value
  * @return string|null
  */
$function ($value): ?string

Examples

Partial Application

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

$pretty = Arrays\toJson(JSON_PRETTY_PRINT);

echo $pretty(['name' => 'Ada', 'age' => 42]);
/*
{

    "name": "Ada",

    "age": 42

}
*/

Details

Arrays Functions

Releated Array cast Functions