Created
February 23, 2018 17:38
-
-
Save san-kumar/0b7d218a08d4d8b4a7d9c07db5a2fbbe to your computer and use it in GitHub Desktop.
sort stories.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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