I have a query
$array1 = $wpdb->get_results( "SELECT timestamp,temp FROM $table_name ORDER BY id desc LIMIT 8", ARRAY_A );
$data1 = json_encode($array1);
echo $data1;
result echoed like this:
[
{"timestamp":"2020-07-26 09:50:25","temp":"26.31"},
{"timestamp":"2020-07-26 09:40:29","temp":"26.37"},
{"timestamp":"2020-07-26 09:30:33","temp":"26.31"},
{"timestamp":"2020-07-26 09:20:37","temp":"26.43"},
{"timestamp":"2020-07-26 09:19:56","temp":null},
{"timestamp":"2020-07-26 08:54:54","temp":"26.37"},
{"timestamp":"2020-07-26 08:44:58","temp":"26.18"},
{"timestamp":"2020-07-26 08:35:02","temp":"26.25"}
]
which I would like to use as input for a graph.
The template given for chartist.js (including also moments.js) is like so:
var chart = new Chartist.Line('.ct-chart', { series: [ {
name: 'series-1',
data: [
{x: new Date(143134652600), y: 53},
{x: new Date(143234652600), y: 40},
{x: new Date(143340052600), y: 45},
{x: new Date(143366652600), y: 40},
{x: new Date(143410652600), y: 20},
{x: new Date(143508652600), y: 32},
{x: new Date(143569652600), y: 18},
{x: new Date(143579652600), y: 11}
]
},
And so on. How to convert this array in php / replace timestamp by x / temp by y and use it (in a loop?) within javascript?
json_encode($array1)right away, loop over the results to create a new array with the desired structure,json_encode()the new array