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

xufengnian_ boke 2022-06-23 15:10:52 阅读数:841

phpspecifiedfieldspositiveorder
 $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://qdmana.com/2022/174/202206231424045267.html