I am trying to create a JSON array from a MySQL database full of sensor values. I am trying to create a multidimensional array from the MySQL results so one query will do all, an thus hopefully reduce server load...?
I want to use the results in highcharts to plot the results from multiple sensor nodes with multiple sensors. I want to plot the results with a time axis.
First I am not sure if a large array with all my data is the correct approach or if multiple smaller arrays are better, any comments welcome on that subject. What I was looking help in was creating a JSON array.
What I have so far is:
<?php
require_once 'db_login.php'; //get login details for the database
$db_server = new mysqli($db_hostname, $db_username, $db_password, $db_database)or die('There was a problem connecting to the database');
$query = "
SELECT sensorID,timeStamp,theDate,theDay,theHour,avgTemp,avgHumidity,avgLight,avgOccupied
FROM
(
select
`sensor_id` AS sensorID,
`value_date` AS timeStamp,
DAY(`value_date`) AS theDate,
DAYNAME(`value_date`) AS theDay,
HOUR(`value_date`) AS theHour,
AVG(`value_temp`) AS avgTemp,
AVG(`value_humidity`) AS avgHumidity,
AVG(`value_light`) AS avgLight,
AVG(`value_PIR`) AS avgOccupied
FROM sensor_value
WHERE `value_date` >= (NOW() - INTERVAL 7 DAY)
AND `value_date` < NOW()
GROUP BY sensorID,theDate,theHour
) s
GROUP BY sensorID,theDate,theHour;
";
$result = $db_server->prepare($query);
$result->execute();
$result->bind_result($sensorID, $timeStamp, $theDate, $theDay, $theHour, $avgTemp, $avgHumidity, $avgLight, $avgOccupied);
while ($result->fetch())
{
//if($theHour<1) $theHour=0.000001;
// $data[$sensorID][$theDate][$theDay][$theHour] = array(
// "Temp" => $avgTemp,
// "Humidity" => $avgHumidity,
// "Light" => $avgLight,
// "Occupied" => $avgOccupied
// );
$data[]['ID'] = array(
"SensorID" => $sensorID,
"DateID" => array(
"Date" => $theDate,
"Day" => $theDay,
"HourID" => array(
"Hour" => $theHour,
"Results" => array(
"Temp" => $avgTemp,
"Humidity" => $avgHumidity,
"Light" => $avgLight,
"Occupied" => $avgOccupied
),
),
),
);
);
}
header("content-type: application/json");
echo json_encode($data, JSON_PRETTY_PRINT);
$db_server->close();
?>
Which gives me:
[
{
"ID": {
"SensorID": 123456789012,
"DateID": {
"Date": 16,
"Day": "Sunday",
"HourID": {
"Hour": 15,
"Results": {
"Temp": "21.000000",
"Humidity": "66.600000",
"Light": "620.6667",
"Occupied": "0.0000"
}
}
}
}
},
{
"ID": {
"SensorID": 123456789012,
"DateID": {
"Date": 16,
"Day": "Sunday",
"HourID": {
"Hour": 16,
"Results": {
"Temp": "21.282203",
"Humidity": "66.494915",
"Light": "668.0763",
"Occupied": "0.1441"
}
}
}
}
},
..........etc.
]
How can I nest the results under the same sensor, then under the same day?
I code that I started with:
while ($result->fetch())
{
$data[$sensorID][$theDate][$theDay][$theHour] = array(
"Temp" => $avgTemp,
"Humidity" => $avgHumidity,
"Light" => $avgLight,
"Occupied" => $avgOccupied
);
}
Had the sort of the correct nesting I wanted but no keys, so I found it hard to actually use the results. Snip-it of above code:
{
"123456789012": {
"16": {
"Sunday": {
"16": {
"Temp": "21.314019",
"Humidity": "66.471028",
"Light": "673.0561",
"Occupied": "0.1402"
},
"17": {
"Temp": "21.927350",
"Humidity": "63.827350",
"Light": "649.4188",
"Occupied": "0.0684"
},
"18": {
"Temp": "22.291525",
"Humidity": "63.710169",
"Light": "615.2712",
"Occupied": "0.0339"
},
"19": {
"Temp": "21.324576",
"Humidity": "64.744068",
"Light": "468.7542",
"Occupied": "0.0085"
},
"20": {
"Temp": "20.801709",
"Humidity": "67.853846",
"Light": "401.1197",
"Occupied": "0.0000"
},
"21": {
"Temp": "20.383051",
"Humidity": "68.610169",
"Light": "183.9407",
"Occupied": "0.0000"
},
"22": {
"Temp": "20.104237",
"Humidity": "69.753390",
"Light": "16.9068",
"Occupied": "0.0000"
},
"23": {
"Temp": "19.939831",
"Humidity": "70.229661",
"Light": "133.5678",
"Occupied": "0.0000"
}
}
},
"17": {
"Monday": [
{
"Temp": "19.728205",
"Humidity": "70.501709",
"Light": "8.0000",
"Occupied": "0.0000"
},
......etc....
]
I have tried adding a foreach loop to nest the data, but could not construct a suitable function that worked...
Can anyone help with creating this array or am I making life hard for myself? Should I move to multiple queries?
EDITED Desired output:
[
{
"ID": {
"SensorID": 123456789012,
"DateID": {
"Date": 16,
"Day": "Sunday",
"HourID": {
"Hour": 17,
"Results": {
"Temp": "21.965217",
"Humidity": "62.708696",
"Light": "649.1449",
"Occupied": "0.0725"
}
"Hour": 18,
"Results": {
"Temp": "22.291525",
"Humidity": "63.710169",
"Light": "615.2712",
"Occupied": "0.0339"
}
"Hour": 19,
"Results": {
"Temp": "21.324576",
"Humidity": "64.744068",
"Light": "468.7542",
"Occupied": "0.0085"
}
}
}
}
},
....etc....
]