-1

So I have an associative array that's 1 level deep (excerpt as below) but there are a lot of entries

[0] => Array
            (
                [Electronic] => 1
                [Scope] => Intruder Alarm Systems                                                                              
                [Issued Date] => 2013-07-23 01:03:41
                [Customer Name] => qqqq
                [Certificate Number] => 1291087
            )

        [1] => Array
            (
                [Electronic] => 1
                [Scope] => CCTV Systems                                                                                        
                [Issued Date] => 2013-07-23 01:02:01
                [Customer Name] => qqqqq
                [Certificate Number] => 1291085
            )

        [2] => Array
            (
                [Electronic] => 1
                [Scope] => CCTV Systems                                                                                        
                [Issued Date] => 2013-07-17 07:15:06
                [Customer Name] => Accent Foundation Ltd
                [Certificate Number] => 1290822
            )

I need a way of looping through this array and re-ordering it so the newest is first and so on. Basically as if you had selected it from a DB and used "ORDER BY "Issued Date" DESC"

I can't really think of a way of doing this.

Steve Smith
  • 734
  • 6
  • 14
  • 29

2 Answers2

1

You can use usort with your own custom function to sort.

usort($array, function ($a, $b) {
     $atime = strtotime($a['Issue Date']);
     $btime = strtotime($b['Issue Date']);
     return $atime - $btime;
});
DevZer0
  • 13,433
  • 7
  • 27
  • 51
0

Always do ordering / sorting while getting data from db itself since the looping structure the way you want to do will be expensive in terms of performance since you are already reading the data from db there itself do the sort its best way.

Vijay
  • 129
  • 11