Skip to content

Core\Db\Row::toArray

$row->toArray(): array<mixed>

Reads a whole row at once, as an array.

Core\Db\Row::toArray returns every column of the row in one array. The key of each entry is the name of the column, and the value is what that column holds. The entries come in the order the query asked for the columns.

A column with no value in this row is present and holds null. A name that is no column of the row is not in the array.

The array is the row's own, so reading from it costs nothing. Writing to it gives your program its own copy, and the row keeps what it read from the database.

Where you know the columns, a typed reader such as Core\Db\Row::int is the better choice. Use toArray in a program that passes the whole row on.

The examples below read a whole row at once, print a row whose columns the program does not know, and answer a request with rows as JSON.

Return value

Returns array<mixed>. An array<mixed> with the column labels as keys. A NULL column is a null entry, and the key is present.

Examples

The examples below run with this configuration file.

nvs.toml
# The `notes` database is kept in memory. It is deleted when the program ends.
[db.notes]
driver = "sqlite"
path = ":memory:"
# The program may open the `notes` database.
[capabilities.db]
connect = ["notes"]

Read the whole row at once

<?nvs
// Shows how to read every column of a row in one step.
var $db = Core\Db::connect('notes');
$db->execute('create table guests (name text not null, seats int not null)', []);
$db->execute('insert into guests (name, seats) values (?, ?)', ['Ada Ahmed', 4]);
array<Core\Db\Row> $rows = $db->query('select name, seats from guests', [])->all();
var $row = $rows[0];
// `toArray` returns the whole row as an array. The key of each entry is the
// name of the column, and the value is what that column holds.
array<mixed> $whole = $row->toArray();
// `as` turns a value into the type this line needs. This prints
// `Ada Ahmed booked 4 seats`.
echo $whole['name'] as ?string ?? '', ' booked ', $whole['seats'] as ?int ?? 0, " seats\n";
$db->close();
Output
Ada Ahmed booked 4 seats

Read a row whose columns you do not know

<?nvs
// Shows how to print a row when the program does not know which columns it
// has. A tool that shows a table works this way.
var $db = Core\Db::connect('notes');
$db->execute(
'create table settings (site text not null, language text not null, page_size int not null, note text)',
[],
);
$db->execute(
'insert into settings (site, language, page_size, note) values (?, ?, ?, ?)',
['example.com', 'en', 25, null],
);
array<Core\Db\Row> $rows = $db->query('select site, language, page_size, note from settings', [])->all();
var $row = $rows[0];
// The array holds the columns in the order the query asked for them. A column
// with no value is in the array too, and its value is `null`.
array<mixed> $whole = $row->toArray();
foreach ($whole as tainted string $name => mixed $value) {
echo $name, ': ', $value ?? '-', "\n";
}
$db->close();
Output
site: example.com
language: en
page_size: 25
note: -

Answer a request with rows as json

<?nvs
// Shows how to turn the rows of a query into JSON, which is what a program
// sends back to a browser or to another service.
var $db = Core\Db::connect('notes');
$db->execute(
'create table products (sku text not null, title text not null, on_hand int not null)',
[],
);
$db->execute(
'insert into products (sku, title, on_hand) values (?, ?, ?), (?, ?, ?)',
['A-1', 'Tea cup', 30, 'A-2', 'Tea pot', 4],
);
array<array<mixed>> $payload = [];
foreach ($db->query('select sku, title, on_hand from products order by sku', []) as Core\Db\Row $row) {
// One call per row gives the array `Core\Json::encode` needs. Every column
// becomes one field of the JSON object, under the name of the column.
$payload[] = $row->toArray();
}
echo Core\Json::encode($payload), "\n";
$db->close();
Output
[{"sku":"A-1","title":"Tea cup","on_hand":30},{"sku":"A-2","title":"Tea pot","on_hand":4}]