I'm trying to plot (with Flot) a pie chart with some data
var data = <?php echo json_encode($data)?>
The result I get from that is this:
var data = [
{"label":"Crear Usuario", "data":"2"},
{"label":"Impresoras", "data":"1"},
{"label":"Problema Correo", "data":"1"},
{"label":"Requisicion Equipo", "data":"1"},
{"label":"Sitio Web", "data":"1"}
]
The problem here is that I need the label
and data
without the quotes, I already tried json_encode($data, JSON_NUMERIC_CHECK);
but only removes the quotes from the numbers.
The following format is what I need:
var data = [
{label:"Crear Usuario",data:2}, ...
First, you have to generate your array in php so the data's value are integers, not strings:
I emulated your array from your json_encode(), I guess it looks like this (or it should):
Then you are missin in Javascript the JSON.parse() to actually make that output into a json object:
The console.log()'s output this for me:
Looks like what you need, am I right?
There's no difference between quoted and unquoted keys. The problem is with the quoting around the actual data values, since Flot requires numbers, not strings.
The json_encode function decides to whether to quote based on the type of data you're giving it. In this case it looks like whatever operations you're performing to create $data are producing string values instead of integers. You need to re-examine those operations, or explicitly tell PHP to interpret them as numbers, using (int) or (float) casting, or the intval/floatval functions.
Try something like this:
and when run
it gives:
I created a class to format json with php without quotes on the keys here is it
you can now call
thanks to Marcin Orlowski