I have this query and I would like to create multidimensional array. I tried this:
$columns = array("col1", "col2", "col3", "col4");
$query = "SELECT " . implode(",", $columns) . " FROM my_table";
$sql = $db->prepare($query);
$sql->execute();
$data = array();
while ($row = $stm->fetch()) {
$nestedData = array();
for ($i = 0, $m = count($columns); $i < $m; $i++) {
$value = $row[$columns[$i]];
$nestedData[] = empty($value) === false ? $value : "";
}
$data[] = $nestedData;
}
I get something like this:
[["value11","value12","value13","value14"],
["value21","value22","value23","value24"], etc]
I would like to have there also names of columns (like this):
[["col1":"value11","col2":"value12","col3":"value13","col4":"value14"],
["col1":"value21","col2":"value22","col3":"value23","col4":"value24"], etc]
Could anybody help me how to achieve this?