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): ClosureWhen 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|\GeneratorThis 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']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']