当前位置:网站首页>PHP specified fields are more than 100 in positive order and less than 100 in random order

PHP specified fields are more than 100 in positive order and less than 100 in random order

2022-06-23 15:10:00 xufengnian_ boke

   $arr = [
           [
               'id'=>1,
               'name'=>'xiaonian',
               'time'=>111,
               'sort'=>100,
           ],
           [
               'id'=>2,
               'name'=>'xiaonian',
               'time'=>111,
               'sort'=>0,
           ],
           [
               'id'=>3,
               'name'=>'xiaogang',
               'time'=>111,
               'sort'=>0,
           ],
           [
               'id'=>4,
               'name'=>'xiaoming',
               'time'=>222,
               'sort'=>100,
           ],
           [
               'id'=>5,
               'name'=>'xiaoming',
               'time'=>222,
               'sort'=>0,
           ],
       ];

        usort($arr, function($a, $b) {
    
            $day['a'] =  $a['time'];
            $day['b'] = $b['time'];
            if ($day['a'] != $day['b']) {
    
                return strtotime($day['a']) - strtotime($day['b']);
            } else {
    
                if ($a['sort'] >= 100 && $b['sort'] >= 100) {
    
                    return $a['id'] - $b['id'];
                } elseif ($a['sort'] < 100 && $b['sort'] < 100) {
    
                    return pow(-1, rand(0,1));
                } else {
    
                    return $b['sort'] - $a['sort'];
                }
            }
        });
原网站

版权声明
本文为[xufengnian_ boke]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231424045267.html