PHP sort weekday and month-year array by custom order

可紊 提交于 2020-02-05 03:31:11

问题


I have an array of weekdays (see below) that I would like to sort as "Mon-Tue-Wed-Thu-Fri-Sat-Sun".

"Sun"=>59
"Sat"=>41
"Fri"=>21
"Thu"=>11
"Wed"=>14
"Tue"=>19
"Mon"=>31

I tried the following code but it doesn't seem to work correctly, the result being the ordered array pasted above, i.e. not in the order I would like it to be.

function orderbyweekday($a, $b) {

    if (strcmp($a, "Mon") == 0) 
        $a = 0;
    else if (strcmp($a, "Tue") == 0) 
        $a = 1;    
    else if (strcmp($a, "Wed") == 0) 
        $a = 2;
    else if (strcmp($a, "Thu") == 0) 
        $a = 3; 
    else if (strcmp($a, "Fri") == 0) 
        $a = 4;
    else if (strcmp($a, "Sat") == 0)  
        $a = 5;
    else if (strcmp($a, "Sun") == 0)   
        $a = 6;    

    if (strcmp($b, "Mon") == 0) 
        $b = 0;
    else if (strcmp($b, "Tue") == 0) 
        $b = 1;    
    else if (strcmp($b, "Wed") == 0) 
        $b = 2;
    else if (strcmp($b, "Thu") == 0) 
        $b = 3; 
    else if (strcmp($b, "Fri") == 0) 
        $b = 4;
    else if (strcmp($b, "Sat") == 0)  
        $b = 5;
    else if (strcmp($b, "Sun") == 0)   
        $b = 6;    

    // if same day, return 0
    if ($a == $b) {
        return 0;
    } 

    return ($a > $b) ? -1 : 1;
}

uksort($movies_per_day, "orderbyweekday");

I would also like to do a similar thing with an array of month-years (e.g. "June 2010"=>10, "May 2009"=>111, etc.), but once I get this right it should be easier.

Thanks a lot.


回答1:


You can try this.

function weekdaySort($a, $b){
      $weekdays = array("Mon", "Tue","Wed","Thu","Fri", "Sat", "Sun");
      return array_search($a, $weekdays) - array_search($b, $weekdays);
} 

uksort($movies_per_day, "weekdaySort");

or if you are using php 5.3 or above you can use a closure;

$weekdays = array("Mon", "Tue","Wed","Thu","Fri", "Sat", "Sun");
uksort($movies_per_day, function($a, $b) use ($weekdays) {return array_search($a, $weekdays) - array_search($b, $weekdays);});

This will avoid recreating the $weekdays array with each iteration.




回答2:


I would recommend to change the array keys to something more computer readable (and sortable), so use numbers as key:

$days = array(6=>59,5=>41,4=>21,3=>11,2=>14,1=>19,0=>31);

You then are able to sort the days by:

ksort($days);

At least for such difficult keys as "June 2010" it is nearly impossible to sort your array. You could use keys like $key = year*12+month;to have a sortable index. "June 2010" could be written as 24125 (2010*12+5), if you use 0 for January.

Or use a timestamp of the first day of a month:

$key = mktime(0,0,0,6,1,2010);

This is more flexible if you want to have a daily report some day.




回答3:


A little late for the accept, but one advantage is that this will work with days of other languages as it uses PHP's date/time handling

$days = array(
    "Sun"=>59,
    "Sat"=>41,
    "Fri"=>21,
    "Thu"=>11,
    "Wed"=>14,
    "Tue"=>19,
    "Mon"=>31,
);
uksort($days, function($a, $b) {

    $c = date_diff(new DateTime('@' . strtotime($a, strtotime('Mon', 0))), new DateTime('@' . strtotime($b, strtotime('Mon', 0))));

    return $c->invert ? 1 : -1;
});

It seems unnecessarily complex for such a simple task, I'm sure there's a better way!




回答4:


This method requires PHP > 5.3:

$arr = array("Sun"=>59, "Thu"=>11, "Sat"=>41, "Fri"=>21, "Tue"=>19, "Wed"=>14, "Mon"=>31);
$ord = array_flip(array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'));
uksort($arr, function ($a, $b) use($ord){
    return $ord[$a] - $ord[$b];
});
var_dump($arr);


来源:https://stackoverflow.com/questions/17678221/php-sort-weekday-and-month-year-array-by-custom-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!