I am running a SQL Query in php and returning the results like this
Name
Joe
Jack
Jerri
Jerry
I need to parse this into a php array and unfortunately, the method I have used in the past to add to a php array is no longer relevant since I am returning multiple rows. How would I alter this syntax in order to account for one column with multiple rows to populate my php array?
$rows = $db->loadRowList();
$output = array();
foreach ($rows as $row) { array_push($output, $row); }
$data = json_encode($output[0]);
TL;DR: Use
loadAssocList('id', 'name')Thus, I'll assume the
$rowsarray looks something like this:You can use PHP's
array_column()to fetch a single index value from every child array:$outputshould now contain an array looking like: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.Note that we now use the
"name"index instead of0.This can be even further improved by utilizing the parameters of
loadAssocList():Using this method, we can receive a single array in
ID => Namekey value format: