Arrays\column()

transformer lazy accepts iterable returns Closure pure
(string, string?) → (Iterable → Iterable)
At a glance — The functional equivalent of array_column(). Lazy — yields one column value per row on demand.

Creates a Closure that extracts a single column value from each row of an array or iterable of rows (arrays or objects). Optionally re-keys the result by another column's value.

/**
  * @param string $column Column to retrieve.
  * @param string|null $key Column to use as the key (null = sequential ints).
  * @return Closure(iterable<int|string, mixed>):(array<int|string, mixed>|\Generator<int|string, mixed>)
  */
Arrays\column(string $column, ?string $key = null): Closure

Returned Closure

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

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

Examples

Partial Application

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

$names = Arrays\column('name');

$rows = [
  ['id' => 1, 'name' => 'Ada'],
  ['id' => 2, 'name' => 'Bea'],
];

print_r($names($rows)); // ['Ada', 'Bea']

Curried

This can be called inline using currying.

print_r(Arrays\column('name', 'id')([
  ['id' => 1, 'name' => 'Ada'],
  ['id' => 2, 'name' => 'Bea'],
]));
// [1 => 'Ada', 2 => 'Bea']

Details

Arrays Functions

Releated Array access Functions