3

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);
  • 3
    If I'm understanding this code's logic, I believe it could all be simplified down to just using `fetchAll()`, as in `$records = $result->fetchAll(PDO::FETCH_ASSOC); echo json_encode($records);` http://php.net/manual/en/pdostatement.fetchall.php because `fetchAll()` will retrieve a 2D assoc array of all records regardless of the column count. – Michael Berkowski May 29 '15 at 13:01
  • You can use loop to do that. – frz3993 May 29 '15 at 13:02
  • Thanks Michael, not even a loop is needed in this case. Great solution. – Marc Van der Smissen May 29 '15 at 13:52

2 Answers2

1

change the switch code segment with this one

$arr_tmp = array();
for($i = 0; $i < count($columns); $i++)
{
    $arr_tmp[$columns[$i]] = $row[$columns[$i]];
}
$records []= $arr_tmp;
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Abdo Adel
  • 1,177
  • 2
  • 17
  • 30
0

You could iterate over the columns:

while ($row = $result->fetch(PDO::FETCH_ASSOC))  {
    $values = array();
    foreach ($columns as $column) {
        values[$column] = $row[$column];
    } 
    records[] = $values;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I like your answer more than Abdo's, although they are basically the same but you are using foreach loop which is little bit more readable. Thanks ! to make it work add $ before values[$column] and records[]. – Marc Van der Smissen May 29 '15 at 13:43