I am working with Highcharts charts by generating a request jQuery.get()
from the chart to the temperature3.php file . For the chart to work properly, the JSON must be in the following format:
[{"item": "minima", "data":[["2015-12-28",8.8],["2015-12-29",10.8],["2015-12-30",15.3]]},
{"item": "maxima", "data":[["2015-12-28",28.7],["2015-12-29",27.5],["2015-12-30",31.1]]}]
The routine (summarized) that I am using to obtain this format is the following:
$minima='{"item": "minima", "data":[';
$maxima='{"item": "maxima", "data":[';
foreach($stm->fetchAll(PDO::FETCH_ASSOC) as $r)
{
$minima.='["'. $r['fecha'].'",'.$r['minima'].'],';
$maxima.='["'. $r['fecha'].'",'.$r['maxima'].'],';
}
$minima.=']}';
$maxima.=']}';
$datos = '['.$minima.','.$maxima.']';
$datos= str_replace(",]", "]", $datos);
echo json_encode($datos,JSON_NUMERIC_CHECK);
The summary table has the following format
id fecha maxima minima
------ ---------- ------ --------
1 2015-12-28 28.7 8.8
2 2015-12-29 27.5 10.8
3 2015-12-30 31.1 15.3
It is effective but not optimal and less efficient. So I guess there must be some less naive way to do it, either through a multidimensional array of objects or some solution that I haven't been able to see.
One option, as you say, would be to define your own multidimensional objects and arrays. For example:
And with that the code above would be simplified to: