Question

I'm trying to export the MySQL table below:

id, asof, value
abc, 2013-06-30, 36000000
abc, 2013-12-31, 48000000
abc, 2014-01-31, 51000000
abc, 2014-02-28, 56000000
xyz, 2013-06-30, 26000000
xyz, 2013-12-31, 33000000
xyz, 2014-01-31, 33000000
xyz, 2014-02-28, 36000000

into the following json format for use in the nvd3.js charts:

[ 
    { 
      "key" : "abc" , 
      "values" : [ [ 2013-06-30, 36000000] , [ 2013-12-31, 48000000] , [ 2014-01-31, 51000000] , [ 2014-02-28, 56000000]
    },
    { 
      "key" : "xyz" , 
      "values" : [ [ 2013-06-30, 26000000] , [ 2013-12-31, 33000000] , [ 2014-01-31, 33000000] , [ 2014-02-28, 36000000]
    }
]

I'm sure this is a newbie question but I'm struggling with it. Any guidance would be much appreciated!

Edit:

I've currently been trying to use an array and while statement but have not been able to figure out how to modify the array to so that it can output to the correct json format.

$query= mysqli_query($db,"SELECT id, asof, value
                          FROM table;"
                        );

if ( ! $query) {
        echo mysqli_error();
        die;
    }

$rows = array();

while($r = mysqli_fetch_assoc($query)) {
    $rows[] = $r;
}

print json_encode($rows);
Was it helpful?

Solution

Probably the most straightforward way of doing this is simply creating a multidimensional array, filling it with data obtained from database and then using json_encode to create a JSON string, which is then sent to the client.

Here is a simple example of a function which will accept an array of items and return a JSON string in the expected format. For simplicity, it is assumed that data was is a 2D array with three columns, but you should modify it to use the format returned by a database query.

function convert($data) {

    $intermediate = array();

    // This intermediate steps is used just to group all rows with
    // the same key
    foreach($data as $item) {
        list($key, $date, $value) = $item;
        $intermediate[$key][] = array($date, $value);
    }

    $output = array();

    foreach($intermediate as $key => $values) {
        $output[] = array(
            'key' => $key,
            'values' => $values
        );
    }

    return $output;
}

Since you're getting data from database, variable $item inside the first foreach statement might actually be an associate array, so you'll have to write something like $item['key'] to get data for columns in the current row.

If you want to use mysqli_fetch_assoc, then you might try calling the function convert in the following way:

$conn = mysqli_connect('', '', '')
$query = 'SQL you wish to execute'
$result = mysqli_query($conn, $query)

if($result) {
    $jsonData = convert($result);
}

However, function itself needs a little bit changing

function convert($result) {

    $intermediate = array();

    while($item = mysqli_fetch_assoc($result)) {
        $key = $item['id'];
        $date = $item['asof'];
        $value = $item['value'];
        $intermediate[$key][] = array($date, $value);
    }

    // The rest of the function stays the same
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top