Search code examples
phpjsonstdclass

How to get the number in string format for json format?


I write this code to output as json format for google chart. however, the return value for number become a string and can't be read by google chart.

$rows = array();
foreach($data as $item) {
$a = new stdClass;
$a->v = $item->location_name;
$a->f = null;

$b = new stdClass;
$b->v = $item->count;
$b->f = null;

$c = new stdClass;
$c->c = array($a, $b);

$rows[] = $c;

}
$cols1 = new stdClass;
$cols1->id = '';
$cols1->label = 'STATE';
$cols1->pattern = '';
$cols1->type = "string";

$cols2 = new stdClass;
$cols2->id = '';
$cols2->label = 'TOTAL';
$cols2->pattern = '';
$cols2->type = "number";

$returndata = new stdClass;
$returndata->cols = array($cols1, $cols2);
$returndata->rows = $rows;

echo json_encode($returndata);

the output become like this: the number should not be in string format

the query is like this:

$sql = "Select ljl.location_id, count(ljj.job_id) as count, ljl.location_name from {local_jobs_job} ljj inner join {local_jobs_location} ljl on ljj.job_location = ljl.location_id group by ljl.location_name";

Solution

  • You can do by cast known "string-number" column to number such as $b->v = intval($item->count); or (int) $item->count