I'm thinking how to group my array by objects with the same value.
I have this result from MySQL query:
Date StartTime EndTime
2014-12-01 08:00 12:00
2014-12-01 10:00 16:00
2014-12-02 12:00 18:00
2014-12-03 10:00 20:00
I have this data in a PHP variable called $Data.
Is it possible to get the json array like this with php: ?
[
{
"2014-12-01": [
{
"StartTime": "08:00",
"EndTime": "12:00"
},
{
"StartTime": "10:00",
"EndTime": "16:00"
}
]
},
{
"2014-12-02": [
{
"StartTime": "12:00",
"EndTime": "18:00"
}
]
},
{
"2014-12-03": [
{
"StartTime": "10:00",
"EndTime": "20:00"
}
]
}
]
If I use echo json_encode($Data), the result is:
[
{
"Date": "2014-12-01",
"StartTime": "10:00",
"EndTime": "16:00"
},
{
"Date": "2014-12-02",
"StartTime": "12:00",
"EndTime": "18:00"
},
{
"Date": "2014-12-03",
"StartTime": "10:00",
"EndTime": "20:00"
}
]
FETCH_GROUPoption tofetchAll()which would produce this structure (more or less).$Data.$Data? Please show a limited sample of it viaprint_r($Data);and also post the PHP code you used to fetch rows from MySQL into$Data.{"2014-12-01": [{"StartTime": "10:00", "EndTime":"16:00"},{"StartTime": "10:00","EndTime": "16:00"}],"2014-12-02":[{"StartTime":"12:00","EndTime":"18:00"}]