fork(29) download
  1. <?php
  2. $data = '{"info":[{"id":1, "title":"original title", "name":"john doe", "date":"2010-05-15"}, {"id":2, "title":"another title", "name":"foo bar", "date":"2009-04-11"}]}';
  3.  
  4. $info = json_decode($data, true)['info'];
  5.  
  6. usort($info, function ($a, $b) {
  7. return $a['date'] <=> $b['date'];
  8. });
  9.  
  10. foreach ($info as $item) {
  11. $date = DateTime::createFromFormat('Y-m-d', $item['date']);
  12. $year = $date->format('Y');
  13. echo "$year {$item['title']}\n";
  14. }
Success #stdin #stdout 0.01s 23964KB
stdin
Standard input is empty
stdout
2009 another title
2010 original title