I'm creating a data.php file which returns a json file to a html file where I fill up a grid with the data from the data.php file.
I need this to be an associative array in the following form:

```
[
 {"CompanyName":"Alfreds Futterkiste","ContactName":"Maria Anders","ContactTitle":"Sales Representative"},
 {"CompanyName":"Ana Trujillo Emparedados y helados","ContactName":"Ana Trujillo","ContactTitle":"Owner"},
 {"CompanyName":"Antonio Moreno Taquera","ContactName":"Antonio Moreno","ContactTitle":"Owner"}
]

```

Now the problem is, I want this data.php to be sort of generic, which means I don't know the columnnames nor the the amount of columns.
The only way I get this done, is by using a switch statement but this is not ideal (because I can make a number of cases but what if the table has one more column) nor is it very elegant.

I bet this can be done far better, any ideas ? 

I tried using array\_push() but that doesn't work with associative arrays.

```
// get columnnames
for ($i = 0; $i < $result->columnCount(); $i++) {
    $col = $result->getColumnMeta($i);
    $columns[] = $col['name'];
}

// fill up array
while ($row = $result->fetch(PDO::FETCH_ASSOC))  {
    switch (count($columns);) {
        case 1 :
            $records[] = array($columns[0] => $row[$columns[0]]);
            break;
        case 2 :
            $records[] = array($columns[0] => $row[$columns[0]], $columns[1] => $row[$columns[1]]);
            break;
        case 3 :
            $records[] = array($columns[0] => $row[$columns[0]], $columns[1] => $row[$columns[1]], $columns[2] => $row[$columns[2]]);
            break;
        case ... // and so on
}   
}

// send data to client
echo json_encode($records);

```