score:17

Accepted answer

json_encode is a convenience method to convert an array into json format. to have the output you provided, you will need an array of arrays. each sub-array has keys "name" and "data", where "name" is the item column, and "data" is another array containing values from 2011 and 2012.

$results = mysql_query("...");
$arr = array();

while ($row = mysql_fetch_assoc($results))
{
    $name = $row['item'];
    $data = array($row['2011'], $row['2012']);

    $arr[] = array('name' => $name, 'data' => $data);
}

echo json_encode($arr);

score:3

  1. loop through the database results and put the results in an array
  2. json encode the array

Related Query

More Query from same tag