Skip to content

Core\Arr::column

Core\Arr::column(
    array<array<T>> $a,
    int|uint|string $column,
    {indexBy?: int|uint|string},
): array<T>

Takes one named value out of every row and returns those values as one array.

Rows are arrays: the rows a database query returned, the lines of a CSV file, a list of records. Core\Arr::column gives you the value of the same key in each row, in the order of the rows. A row that does not have that key is skipped.

You can also key the result by a second value. Write indexBy with the key you want, and each value lands under the value of that cell. Two rows with the same key give one entry, and the last row wins.

The examples below show one value from every row, a list where some rows do not have the key, and a lookup table built with indexBy.

Parameters

  • $aarray<array<T>>The rows. Each row is an array, and each entry of a row is a cell.
  • $columnint|uint|stringThe key of the cell to take; 0 and "0" name one cell, as $row[0] and $row["0"] do.

Options

  • indexByint|uint|stringoptionalThe key of the cell to use as the result's key. By default the result gets new integer keys.

Return value

Returns array<T>. The cells in row order. A row without the column is skipped. A row without the indexBy cell goes under the next free integer key. When two rows have the same indexBy value, the last one is kept.

Changelog

VersionChange
0.0.6A Core\Arr function that takes a key, such as hasKey, gave a compile error for a uint key (an unsigned whole number). It now takes a uint key, as $a[$key] already did. So Core\Arr::fillKeys($ids, true) works when $ids is a list of uint values.

Examples

One value from every row

<?nvs
// Shows how to take one value out of every row. `Core\Arr::column` returns the
// value of the key you name from each row, in the order of the rows.
array<array<string>> $users = [
['id' => '7', 'name' => 'Ada'],
['id' => '9', 'name' => 'Bo'],
['id' => '12', 'name' => 'Cy'],
];
array<string> $names = Core\Arr::column($users, 'name');
// This prints Ada, Bo and Cy, one name per line.
foreach ($names as string $name) {
echo $name, "\n";
}
Output
Ada
Bo
Cy

The rows that have the value

<?nvs
// Shows what happens when a row does not have the key. That row is skipped, so
// this collects only the addresses that people really gave.
array<array<string>> $people = [
['name' => 'Ada', 'email' => 'ada@example.com'],
['name' => 'Bo'],
['name' => 'Cy', 'email' => 'cy@example.com'],
];
array<string> $addresses = Core\Arr::column($people, 'email');
// This prints 2. Bo gave no address, so the result has no entry for Bo.
echo Core\Arr::count($addresses), "\n";
// This prints the two addresses, one per line.
foreach ($addresses as string $address) {
echo $address, "\n";
}
Output
2
ada@example.com
cy@example.com

A lookup table of names

<?nvs
// Shows how to build a lookup table from rows. A page does this when it has the
// rows of one query and has to find a row by its id.
array<array<string>> $rows = [
['id' => '7', 'name' => 'Ada'],
['id' => '9', 'name' => 'Bo'],
['id' => '12', 'name' => 'Cy'],
];
// `indexBy` names the key whose value becomes the key of the result. Here that
// is ['7' => 'Ada', '9' => 'Bo', '12' => 'Cy'].
array<string> $nameById = Core\Arr::column($rows, 'name', { indexBy: 'id' });
array<string> $shown = ['12', '7', '9'];
// This prints Cy, Ada and Bo, one name per line.
foreach ($shown as string $id) {
echo $nameById[$id], "\n";
}
Output
Cy
Ada
Bo