Arrays\last()

reducer terminal accepts iterable returns value pure
Iterable → T | null
At a glance — Direct call — not a Closure constructor. Terminal — must consume the entire source. Do not use with infinite Generators.

Returns the last value of an array or iterable, or null if the source is empty. For a Generator the whole stream is consumed (there is no way to know the last value without doing so).

/**
  * @param iterable<int|string, mixed> $source
  * @return mixed The last value, or null if empty.
  */
Arrays\last(iterable $source)

Examples

Partial Application

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

var_dump(Arrays\last(['a', 'b', 'c'])); // 'c'
var_dump(Arrays\last([]));              // NULL

Details

Arrays Functions

Releated Array access Functions