Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fabricekabongo/cd21889659ced10d51c38a911c6dac1f to your computer and use it in GitHub Desktop.
Save fabricekabongo/cd21889659ced10d51c38a911c6dac1f to your computer and use it in GitHub Desktop.
$data = '[{"id":1,"first_name":"Halli","last_name":"Serridge","date":"2017-01-31","location":"Prosyana"},
{"id":2,"first_name":"Henrieta","last_name":"Salzburg","date":"2016-08-20","location":"Antalaha"},
{"id":3,"first_name":"Matteo","last_name":"Fogt","date":"2016-04-15","location":"Fucheng"},
{"id":4,"first_name":"Gray","last_name":"Trosdall","date":"2016-08-15","location":"Yinglong"},
{"id":5,"first_name":"Jerome","last_name":"Edmonson","date":"2017-05-24","location":"San Jose"},
{"id":6,"first_name":"Else","last_name":"Massie","date":"2016-07-14","location":"Tours"},
{"id":7,"first_name":"Marleah","last_name":"Tamblyn","date":"2017-07-29","location":"Kazimierz Dolny"},
{"id":8,"first_name":"Richy","last_name":"Nowaczyk","date":"2017-05-24","location":"Torsby"},
{"id":9,"first_name":"Yuma","last_name":"Torn","date":"2016-10-20","location":"Veruela"},
{"id":10,"first_name":"Leann","last_name":"Giberd","date":"2017-05-31","location":"Najin"}]';
$candidates = json_decode($data, true);
$table = [];
$columnHeaders = ['Weeks'];
foreach ($candidates as $candidate) {
$date = new \DateTime($candidate['date']);
if (!isset($table [$date->format('W')])) {
$table[$date->format('W')] = [];
}
if (!isset($table [$date->format('W')][$candidate['location']])) {
$table[$date->format('W')][$candidate['location']] = 0;
}
$table [$date->format('W')][$candidate['location']] += 1;
if (!in_array($candidate['location'], $columnHeaders)) {
$columnHeaders[] = $candidate['location'];
}
}
echo '<table><tr><td>';
echo implode('</td><td>', $columnHeaders);
echo '</td></tr>';
foreach($table as $week => $row) {
echo '<tr><td>' . $week . '</td>';
foreach($columnHeaders as $location) {
echo '<td>';
echo isset($row[$location])?$row[$location]:0;
echo '</td>';
}
echo '</tr>';
}
//voila
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment