Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort array based on the dateTime in php

Array         (             [0] => Array                 (                     [dateTime] => 2011-10-18 0:0:00                     [chanl1] => 20.7                     [chanl2] => 45.4                     [chanl3] =>                  )              [1] => Array                 (                     [dateTime] => 2011-10-18 0:15:00                     [chanl1] => 20.7                     [chanl2] => 45.4                     [chanl3] =>                  )               [2] => Array                 (                     [dateTime] => 2011-10-18 00:14:00                     [chanl1] => 20.7                     [chanl2] => 33.8                     [chanl3] =>                  )              [3] => Array                 (                     [dateTime] => 2011-10-18 00:29:00                     [chanl1] => 20.6                     [chanl2] => 33.9                     [chanl3] =>                  ) 

I want to sort the above array based on the [dateTime], the final output should be:

Array         (             [0] => Array                 (                     [dateTime] => 2011-10-18 0:0:00                     [chanl1] => 20.7                     [chanl2] => 45.4                     [chanl3] =>                  )              [1] => Array                 (                     [dateTime] => 2011-10-18 00:14:00                     [chanl1] => 20.7                     [chanl2] => 33.8                     [chanl3] =>                  )              [2] => Array                 (                     [dateTime] => 2011-10-18 0:15:00                     [chanl1] => 20.7                     [chanl2] => 45.4                     [chanl3] =>                  )                          [3] => Array                 (                     [dateTime] => 2011-10-18 00:29:00                     [chanl1] => 20.6                     [chanl2] => 33.9                     [chanl3] =>                  ) 

Is there anyone know how to do it? Thanks!

like image 500
Acubi Avatar asked Nov 14 '11 11:11

Acubi


People also ask

How do I sort an array of dates in php?

Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function.

How do I sort a 2d array in php?

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

How do you sort an array of arrays in php?

To PHP sort array by key, you should use ksort() (for ascending order) or krsort() (for descending order). To PHP sort array by value, you will need functions asort() and arsort() (for ascending and descending orders).

What is Ksort php?

The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value.


1 Answers

Use usort() function with custom comparator:

$arr = array(...);  usort($arr, function($a, $b) {   $ad = new DateTime($a['dateTime']);   $bd = new DateTime($b['dateTime']);    if ($ad == $bd) {     return 0;   }    return $ad < $bd ? -1 : 1; }); 

DateTime class has overloaded comparison operators (<, >, ==).

like image 68
Crozin Avatar answered Sep 28 '22 04:09

Crozin