TL;DR: Use loadAssocList('id', 'name')
loadRowList()
returns an indexed array of indexed arrays from the table records returned by the query
Thus, I'll assume the $rows
array looks something like this:
Array
(
[0] => Array
(
[0] => Joe
)
[1] => Array
(
[0] => Jack
)
[2] => Array
(
[0] => Jerri
)
[3] => Array
(
[0] => Jerry
)
)
You can use PHP's array_column()
to fetch a single index value from every child array:
$rows = $db->loadRowList();
$output = array_column($rows, 0);
$output
should now contain an array looking like:
Array
(
[0] => Joe
[1] => Jack
[2] => Jerri
[3] => Jerry
)
An improvement we can make here is to maintain an associative array, that maps your database's column names to their values. loadAssocList()
can help there.
$rows = $db->loadAssocList();
$output = array_column($rows, 'name');
Note that we now use the "name"
index instead of 0
.
This can be even further improved by utilizing the parameters of loadAssocList()
:
loadAssocList($key, $column)
returns an associative array, indexed on 'key', of values from the column named 'column' returned by the query
Using this method, we can receive a single array in ID => Name
key value format:
$ouput = $db->loadAssocList('id', 'name');