I'm thinking how to group my array by objects with the same date. I have this result from MySQL query(select DateTime ,ip,Val from my table):
DateTime ip Val
2017-02-01 08:00:23 10.10.10.1 2
2017-02-01 09:01:23 10.10.10.2 3
2017-02-01 12:02:23 10.10.10.3 4
2017-02-02 13:03:23 10.10.10.4 5
2017-02-03 15:04:23 10.10.10.5 6
2017-02-03 20:05:23 10.10.10.6 7
from mysql query result to create json array object as below ,
$sql = "select DateTime ,ip,Val from my table order by DateTime ASC ;";
$result = $db->query($sql);
$data = array();
$rowary = array();
$i=0;
while($row = mysqli_fetch_array($result))
{
$rowary['DateTime'] = $row['DateTime '] ;
$rowary['ip'] = $row['ip'] ;
$rowary['Val'] = $row['Val'] ;
$data[$i++]=$rowary;
}
echo '<pre>' . var_export($data, true) . '</pre>';
Is it possible to create a JSON array object that group by the same date If I use
var data = <?php echo json_encode($Data, JSON_PRETTY_PRINT);?>
in javascript,
expected result is:
[
[
{
"DateTime": "2017-02-01 08:00:23",
"ip": "10.10.10.1",
"Val": "2"
},
{
"DateTime": "2017-02-01 09:01:23",
"ip": "10.10.10.2",
"Val": "3"
},
{
"DateTime": "2017-02-01 12:02:23",
"ip": "10.10.10.3",
"Val": "4"
}
],
[
{
"DateTime": "2017-02-02 13:03:23",
"ip": "10.10.10.4",
"Val": "5"
}
],
[
{
"DateTime": "2017-02-03 15:04:23",
"ip": "10.10.10.5",
"Val": "6"
},
{
"DateTime": "2017-02-03 20:05:23",
"ip": "10.10.10.6",
"Val": "7"
}
]
]
How can I create a JSON array object that group by the same date?