Last active
February 7, 2023 16:51
-
-
Save sohelrana820/861c36a511282335ce85 to your computer and use it in GitHub Desktop.
How to sort PHP multidimensional array by timestamp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Feeds array with title and time | |
$feeds = array( | |
array( | |
'title' => 'Some Feeds.', | |
'time' => '2015-03-31 13:06:00' | |
), | |
array( | |
'title' => 'And another.', | |
'time' => '2015-03-23 13:06:00' | |
), | |
array( | |
'title' => 'Another Feed.', | |
'time' => '2015-03-27 13:06:00' | |
), | |
array( | |
'title' => 'And one more feed.', | |
'time' => '2015-03-20 13:06:00' | |
), | |
array( | |
'title' => 'And one more feed.', | |
'time' => '2015-03-25 13:06:00' | |
) | |
); | |
// Sorting array by time (DESC ORDER) | |
usort($feeds, function($firstItem, $secondItem) { | |
$timeStamp1 = strtotime($firstItem['time']); | |
$timeStamp2 = strtotime($secondItem['time']); | |
return $timeStamp2 - $timeStamp1; | |
}); | |
var_dump($feeds); | |
// Sorting array by time (ASC ORDER) | |
usort($feeds, function($firstItem, $secondItem) { | |
$timeStamp1 = strtotime($firstItem['time']); | |
$timeStamp2 = strtotime($secondItem['time']); | |
return $timeStamp1 - $timeStamp2; | |
}); | |
var_dump($feeds); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment