fork download
  1. <?php
  2. $hits = [
  3. ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 1],
  4. ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 4],
  5. ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 2],
  6. ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 1],
  7. ['date' => new \DateTime('2019-01-01 12:00:00'), 'roundIndex' => 1],
  8. ['date' => new \DateTime('2019-01-04 12:00:00'), 'roundIndex' => 4],
  9. ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 2],
  10. ['date' => new \DateTime('2019-01-23 12:00:00'), 'roundIndex' => 1],
  11. ['date' => new \DateTime('2019-01-22 12:00:00'), 'roundIndex' => 6],
  12. ['date' => new \DateTime('2019-01-01 12:00:00'), 'roundIndex' => 3],
  13. ['date' => new \DateTime('2019-01-06 12:00:00'), 'roundIndex' => 2],
  14. ['date' => new \DateTime('2019-01-28 12:00:00'), 'roundIndex' => 7],
  15. ['date' => new \DateTime('2019-01-26 12:00:00'), 'roundIndex' => 3],
  16. ['date' => new \DateTime('2019-01-27 12:00:00'), 'roundIndex' => 5],
  17. ['date' => new \DateTime('2019-01-26 12:00:00'), 'roundIndex' => 2],
  18. ['date' => new \DateTime('2019-01-11 12:00:00'), 'roundIndex' => 4],
  19. ['date' => new \DateTime('2019-01-24 12:00:00'), 'roundIndex' => 3],
  20. ['date' => new \DateTime('2019-01-08 12:00:00'), 'roundIndex' => 7],
  21. ['date' => new \DateTime('2019-01-11 12:00:00'), 'roundIndex' => 8],
  22. ['date' => new \DateTime('2019-01-14 12:00:00'), 'roundIndex' => 6],
  23. ['date' => new \DateTime('2019-01-13 12:00:00'), 'roundIndex' => 4],
  24. ['date' => new \DateTime('2019-01-13 12:00:00'), 'roundIndex' => 5],
  25. ['date' => new \DateTime('2019-01-24 12:00:00'), 'roundIndex' => 2],
  26. ['date' => new \DateTime('2019-01-27 12:00:00'), 'roundIndex' => 4],
  27. ];
  28. usort($hits, static function(array $item1, array $item2) {
  29. return $item1['date'] < $item2['date'] ? -1 : 1;
  30. });
  31.  
  32. // so far it works
  33.  
  34.  
  35. // now this screws up
  36. usort($hits, function($a,$b) use ($defaults) {
  37. if (
  38. $a['date']->format('Y-m-d H:i:s') === $b['date']->format('Y-m-d H:i:s'))
  39. {
  40. if ($a['roundIndex'] > $b['roundIndex'])
  41. {
  42. return 1;
  43. }
  44. else
  45. {
  46. return -1;
  47. }
  48. }
  49. return 1;
  50. });
  51.  
  52. /* if you remove the sort() above and uncomment this manual sort, it will ALL work fine, but I dont know why....
  53. $sizeOfHits = sizeof($hits);
  54. for($i = 0; $i < $sizeOfHits; $i++)
  55. {
  56.   for($j = $i+1; $j < $sizeOfHits; $j++)
  57.   {
  58.   if (
  59.   $hits[$i]['date']->format('Y-m-d H:i:s') === $hits[$j]['date']->format('Y-m-d H:i:s'))
  60.   {
  61.   if ($hits[$i]['roundIndex'] > $hits[$j]['roundIndex'])
  62.   {
  63.   $x = $hits[$j];
  64.   $hits[$j] = $hits[$i];
  65.   $hits[$i] = $x;
  66.   }
  67.   }
  68.   }
  69. }*/
  70.  
  71. foreach($hits as $hit) { echo $hit['date']->format('Y-m-d H:i:s').' '.$hit['roundIndex']."\r\n"; }
Success #stdin #stdout #stderr 0.02s 24880KB
stdin
Standard input is empty
stdout
2019-01-08 12:00:00 7
2019-01-04 12:00:00 4
2019-01-06 12:00:00 2
2019-01-02 12:00:00 1
2019-01-02 12:00:00 1
2019-01-02 12:00:00 2
2019-01-02 12:00:00 2
2019-01-02 12:00:00 4
2019-01-11 12:00:00 4
2019-01-11 12:00:00 8
2019-01-27 12:00:00 4
2019-01-26 12:00:00 3
2019-01-24 12:00:00 2
2019-01-26 12:00:00 2
2019-01-23 12:00:00 1
2019-01-24 12:00:00 3
2019-01-22 12:00:00 6
2019-01-14 12:00:00 6
2019-01-13 12:00:00 4
2019-01-13 12:00:00 5
2019-01-01 12:00:00 3
2019-01-27 12:00:00 5
2019-01-28 12:00:00 7
2019-01-01 12:00:00 1
stderr
PHP Notice:  Undefined variable: defaults in /home/a2jKzJ/prog.php on line 36