Skip to content

Instantly share code, notes, and snippets.

@beingsane
Forked from isaqueprofeta/json_grouping.php
Created April 17, 2020 18:25
Show Gist options
  • Save beingsane/788d089c63c3915d169afe96b178d94b to your computer and use it in GitHub Desktop.
Save beingsane/788d089c63c3915d169afe96b178d94b to your computer and use it in GitHub Desktop.
<?php
$in = <<<'JSON'
[
{
"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"
},
{
"Date": "2014-12-03",
"StartTime": "12:00",
"EndTime": "20:00"
}
]
JSON;
$data = json_decode($in, true);
$out = [];
foreach($data as $element) {
$out[$element['Date']][] = ['StartTime' => $element['StartTime'], 'EndTime' => $element['EndTime']];
}
var_dump(json_encode($out, JSON_PRETTY_PRINT));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment