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
  • 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); php.net/manual/en/pdostatement.fetchall.php because fetchAll() will retrieve a 2D assoc array of all records regardless of the column count. Commented May 29, 2015 at 13:01
  • You can use loop to do that. Commented May 29, 2015 at 13:02
  • Thanks Michael, not even a loop is needed in this case. Great solution. Commented May 29, 2015 at 13:52

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

Comments

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;
}

1 Comment

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[].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.