1

What I am trying to do, is pass a database query's result set directly into a Google DataTable. (Google visualization api here).

My goal (at least this is the way I can think of to use it, is to use Google's ArrayToDataTable() function. For this, I need my query to return a two dimensional array that I can pass to this function. I was originally trying to do this using PDO::FETCH_ASSOC, but when I encoded the result into JSON, I got an array of row objects that google's API couldn't handle. I then found this question, and using PDO::FETCH_NUM, I get a 2 dimensional array, which is a lot closer to what I want, but it omits column headers.

Is there a simple way to get a 2 dimensional array whose first entry is the column names, and each subsequent entry is a row from a database query? I would think this would be trivial, but I can't find a solution, without writing a function to do it.

Thank you, -Eric

EDIT:

In response to Michael's answer, I wrote this:

function assocTo2dArray($array){
    $return = array();
    $return[] = array_keys($array[0]);
    foreach($array as $row){
        $return[] = array_values($row);
    }
    return $return;
}

which achieves the same result, but looks a little cleaner.

2
  • Are you writing the SQL query yourself? Feb 6, 2014 at 21:54
  • Yes, I am writing my own query.
    – EpicWally
    Feb 6, 2014 at 22:00

1 Answer 1

1

This assumes you've already built and executed a prepared statement (named $stmt).

$results = array();

$n = 0;
$fetchmode = PDO::FETCH_ASSOC;
while (true) {
    $row = $stmt->fetch($fetchmode);

    if ($row == null)
        break;

    if ($n == 0) {
        $results[0] = array(); //headers
        $results[1] = array(); //first row
        foreach ($row as $k => $v) {
            $results[0][] = $k;
            $results[1][] = $v;
        }

        $fetchmode = PDO::FETCH_NUM;
    } else {
        $results[] = $row;
    }

    $n++;
}

This SHOULD do what you are looking for.

3
  • This works, and I figured something like this could be done. I was just hoping there was a cleaner way, like a fetch_style that would do it as a part of the query, rather than having to query and then manipulate the result set.
    – EpicWally
    Feb 6, 2014 at 22:08
  • Looking at the PDOStatement functions, I don't see a concise method. It would have to be a fetchAll type function, since you only want to include headers once. You could start the results array with the column headings as the first row "manually", but who wants that? And of course, any changes to the SQL and you have multiple places to change it! Feb 6, 2014 at 22:19
  • Also, there seems to be a much cleaner way of doing it. (EDIT I can't figure out how to make this display in code in the comment... Including it in original post.)
    – EpicWally
    Feb 6, 2014 at 22:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.