-1

I have an array whose keys are date which i want to sort in ascending order. I tried sorting it out with ksort() and other sort method which didn't work out. Sorting out with usort() also didn't work.

this is my array

array:7 [▼
  "21-07-2017" => array:1 [▶]
  "04-09-2017" => array:3 [▶]
  "27-07-2017" => array:1 [▶]
  "31-07-2017" => array:1 [▶]
  "01-08-2017" => array:2 [▶]
  "05-09-2017" => array:1 [▶]
  "15-09-2017" => array:1 [▶]
]

this is what i did.

usort($date, function($a, $b) {
          foreach ($a as $value1) {
              foreach ($b as $value2) {
                  return ($value1['date'] < $value2['date']) ? -1 : 1;
              }
          }
        });

Are there any in built functions in php which i am missing ?

Naveen Kumar
  • 1,476
  • 5
  • 28
  • 52
  • ksort should do the trick, can you show what you are expecting the outcome to look like. – i-man Sep 14 '17 at 16:55
  • There is already a post concerning how to sort a date array right here : https://stackoverflow.com/questions/40462778/how-to-sort-date-array-in-php – Arrabidas92 Sep 14 '17 at 16:55
  • 2
    Possible duplicate of [How to sort date array in php?](https://stackoverflow.com/questions/40462778/how-to-sort-date-array-in-php) – Qirel Sep 14 '17 at 16:57
  • Is ans provided worked for you ? – Niklesh Raut Sep 15 '17 at 11:22
  • ksort can't properly sort your dates because they are in d-m-Y format, but ksort can only treat them as strings, so it will sort on the first number ie the day of the month, not the number of the month. That is why user2486 has the good solution which converts each key into a timestamp, then sorts them. – i-man Sep 15 '17 at 15:57

2 Answers2

1

Convert date to string by strtotime function and then sort by key using ksort

 $temp = array();
 foreach($arr as $key=>$value){
     $temp[strtotime($key)] = $value;
 }
 ksort($temp);
 $new = array();
 foreach($temp as $key=>$value){
      $new[date("d-m-Y",$key)] = $value;
 }
 print_r($new);

Live demo : https://eval.in/862064

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • @salathe : I have upated – Niklesh Raut Sep 15 '17 at 11:21
  • instead of creating new arrays, why not just do the timestamp conversion and comparison in the usort function, so that it will maintain the original array the OP is hoping for? – i-man Sep 15 '17 at 15:59
  • @i-man : brother I may be wrong, but I think he just tried with `usort` not hard and fast to use only `usort` . Although `usort` is to sort array by value not by key. It would be wonder if you provide as you are saying. – Niklesh Raut Sep 15 '17 at 16:10
  • @user2486 I upvoted your answer as I think it should be accepted, and agree with you, but was just thinking maybe the OP will accept it if the original array is sorted – i-man Sep 15 '17 at 16:15
  • I used the same array , OP is not given complete array. – Niklesh Raut Sep 15 '17 at 16:19
0

Your date values are not formatted in Y-m-d -- a format that can be simply sorted naturally as a string value. When you don't have date/time units ordered from largest to smallest, then you will need to parse the string and convert it into a format that can be compared correctly.

Your date format is already in European format and strtotime() will parse that value correctly. Use uksort() to compare values after they have been converted to unix times. This will sort your array ASC without modifying the original keys.

Code: (Demo)

$array = [
    "21-07-2017" => ['a'],
    "04-09-2017" => ['b'],
    "27-07-2017" => ['c'],
    "31-07-2017" => ['d'],
    "01-08-2017" => ['e'],
    "05-09-2017" => ['f'],
    "15-09-2017" => ['g'],
];

uksort($array, function($a, $b) { return strtotime($a) <=> strtotime($b);});

var_export($array);

Output:

array (
  '21-07-2017' => 
  array (
    0 => 'a',
  ),
  '27-07-2017' => 
  array (
    0 => 'c',
  ),
  '31-07-2017' => 
  array (
    0 => 'd',
  ),
  '01-08-2017' => 
  array (
    0 => 'e',
  ),
  '04-09-2017' => 
  array (
    0 => 'b',
  ),
  '05-09-2017' => 
  array (
    0 => 'f',
  ),
  '15-09-2017' => 
  array (
    0 => 'g',
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136