Ninjakreborn Posted May 14, 2010 Share Posted May 14, 2010 I am probably using it wrong but this isn't working. function user_sort($a, $b) { if (strtotime($a['news_date']) > strtotime($a['news_date'])) { return 1; } } uasort($data, user_sort); The data object looks like this. Any one can help me figure out why this isn't working? I basically just want to short the array by the "news_date" value with the newest values showing up first. Array ( [427] => Array ( [news_id] => 427 [company_id] => 16 [news_date] => 05/13/2010 [content] => [expiration_date] => 06/30/2010 [is_active] => 1 [is_featured] => 0 [is_news] => 1 [is_portfolio] => 1 [featured_description] => [created_date] => 05/22/2010 [short_title] => [sortOrder] => 1 [hide_news_page] => 0 ) [505] => Array ( [news_id] => 505 [company_id] => 72 [news_date] => 05/15/2010 [abstract] => [content] => [expiration_date] => [is_active] => 1 [is_featured] => 0 [is_news] => 1 [is_portfolio] => 1 [featured_description] => [created_date] => 0000-00-00 [short_title] => [sortOrder] => 2 [hide_news_page] => 0 ) [439] => Array ( [news_id] => 439 [company_id] => 38 [news_date] => 2009-09-08 00:00:00 [content] => [expiration_date] => [is_active] => 1 [is_featured] => 0 [is_news] => 1 [is_portfolio] => 1 [featured_description] => [created_date] => 0000-00-00 [short_title] => testdata [sortOrder] => 3 [hide_news_page] => ) Quote Link to comment https://forums.phpfreaks.com/topic/201754-array-uasort-not-doing-what-i-want/ Share on other sites More sharing options...
Ninjakreborn Posted May 14, 2010 Author Share Posted May 14, 2010 I also just tried something along the lines of: <?php //>> GET DATA AND SET LOCAL DATA OBJECTS $objDataCollection = $objData->get_news(); function user_sort($a, $b) { $date1 = strtotime($a['news_date']); $date2 = strtotime($a['news_date']); if ($date1 == $date2) return 0; return ($date1 > $date2) ? -1 : 1; } uasort($objDataCollection, user_sort); ?> <?php //>> GET DATA AND SET LOCAL DATA OBJECTS $objDataCollection = $objData->get_news(); function user_sort($a, $b) { //$date1 = strtotime($a['news_date']); //$date2 = strtotime($a['news_date']); if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } uasort($objDataCollection, user_sort); ?> This almost works but not entirely...for some reason some dates are off. Any advice? Quote Link to comment https://forums.phpfreaks.com/topic/201754-array-uasort-not-doing-what-i-want/#findComment-1058294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.