usort($people, function($a, $b) {
// 先按照年龄升序排序 if ($a['age'] == $b['age']) {
// 如果年龄相同,则按照名字字母顺序升序排序 return strcmp($a['name'], $b['name']);
}
return $a['age'] - $b['age'];
});
// self::myOrder($list,'col1', ['c']); //调用
public static function myOrder(&$list, $field, $orderList)
{
$len = count($orderList);
usort($list, function ($a, $b) use ($field,$orderList, $len) {
$aIndex = array_search($a[$field], $orderList);
if ($aIndex === false) {
$aIndex = $len;
}
$bIndex = array_search($b[$field], $orderList);
if ($bIndex === false) {
$bIndex = $len;
}
return $aIndex - $bIndex;
});
}
网友评论