Skip to content

Instantly share code, notes, and snippets.

@san-kumar
Created February 23, 2018 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save san-kumar/0b7d218a08d4d8b4a7d9c07db5a2fbbe to your computer and use it in GitHub Desktop.
Save san-kumar/0b7d218a08d4d8b4a7d9c07db5a2fbbe to your computer and use it in GitHub Desktop.
sort stories.json
<?php
$data = json_decode(file_get_contents('c:/tmp/stories.json'), true);
$map = ['year', 'created_at', 'title', 'url', 'points', 'author', 'num_comments'];
$file_input = fopen("stories.csv", "w");
fputcsv($file_input, $map);
foreach($data as $year => $stories) {
foreach($stories as $story) {
for($row = [$year], $i = 1; $i < count($map); $i++) {
$row[$map[$i]] = $i == 1 ? date('Y-m-d H:i:s', strtotime($story['created_at'])) : $story[$map[$i]];
}
fputcsv($file_input, $row);
}
}
fclose($file_input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment