1.

How To Use Json_encode?

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

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



Discussion

No Comment Found