I have this json string as datatable to draw a chart:
{"cols":[
{"id":"","label":"Month","pattern":"","type":"string"},
{"id":"","label":"Job","pattern":"","type":"number"},
{"id":"","label":"Internship","pattern":"","type":"number"}],
"rows":[
{"c":[{"v":"July","f":null},{"v":6,"f":null},{"v":2,"f":null}]},
{"c":[{"v":"August","f":null},{"v":0,"f":null},{"v":3,"f":null}]}]}
The json string is derived from this array (the values are from sql query):
$rows = array_map(function($item) {
return (object) ['c' => [
(object) ['v' => $item->month, 'f' => null],
(object) ['v' => intval($item->jobcount), 'f' => null],
(object) ['v' => intval($item->interncount), 'f' => null]
]];
}, array_values($data));
// prepare return data
$cols = [
(object) ['id' => '', 'label' => 'Month', 'pattern' => '', 'type' => 'string'],
(object) ['id' => '', 'label' => 'Job', 'pattern' => '', 'type' => 'number'],
(object) ['id' => '', 'label' => 'Internship', 'pattern' => '', 'type' => 'number'],
];
$returndata = new stdClass;
$returndata->cols = $cols;
$returndata->rows = $rows;
echo json_encode($returndata);
I want to insert another months from January to December with 0 values for jobcount and interncount into the json string.
How to put this array into the json string so that the other months will be included into the chart?
$months = array('January', 'February', 'March', 'April', 'May', 'Jun', 'July', 'August', 'September', 'October', 'November', 'December');
This is the chart.