方法一:
直接排序不返回首字母
$old_list = $this->model->field('id, number, username, mobile')
->where('xxx', 'xxx')
->select();
foreach ($old_list as $key => $value) {
$value['username'] = iconv('UTF-8', 'GBK', $value['username']);
$new_list[$key] = $value;
}
array_multisort(array_column($new_list, 'username'), SORT_ASC, $new_list);
foreach ($new_list as $key => $value) {
$value['username'] = iconv('GBK', 'UTF-8', $value['username']);
$list [$key] = $value;
}
方法二:
返回首字母的排序
public function test() {
$list = $this->model->field('id, number, username, mobile')
->where('xxx', 'xxx')
->select();
foreach ($list as $name) {
$char = $this->getFirstChar($name['username']);
$nameArray = array();
if (isset($charArray[$char]) && count($charArray[$char]) != 0) {
$nameArray = $charArray[$char];
}
array_push($nameArray, $name);
$charArray[$char] = $nameArray;
}
ksort($charArray);
echo '<pre>';
var_dump($charArray);
}
public function getFirstChar($s)
{
$s0 = mb_substr($s, 0, 3); //获取名字的姓
$s = iconv('UTF-8', 'gb2312', $s0); // 将UTF-8转换成GB2312编码
if (ord($s0) > 128) { //汉字开头,汉字没有以U、V开头的
$asc = ord($s[0]) * 256 + ord($s[1]) - 65536;
#################################################
### 如果没有对应首字母 打印 $asc 的值 ###
### 在 FamilyNamesRareWords 方法中 ###
### $rare_arr 数组中手动添加对应首字母即可 ###
#################################################
if ($asc >= -20319 and $asc <= -20284) return "A";
if ($asc >= -20283 and $asc <= -19776) return "B";
if ($asc >= -19775 and $asc <= -19219) return "C";
if ($asc >= -19218 and $asc <= -18711) return "D";
if ($asc >= -18710 and $asc <= -18527) return "E";
if ($asc >= -18526 and $asc <= -18240) return "F";
if ($asc >= -18239 and $asc <= -17760) return "G";
if ($asc >= -17759 and $asc <= -17248) return "H";
if ($asc >= -17247 and $asc <= -17418) return "I";
if ($asc >= -17417 and $asc <= -16475) return "J";
if ($asc >= -16474 and $asc <= -16213) return "K";
if ($asc >= -16212 and $asc <= -15641) return "L";
if ($asc >= -15640 and $asc <= -15166) return "M";
if ($asc >= -15165 and $asc <= -14923) return "N";
if ($asc >= -14922 and $asc <= -14915) return "O";
if ($asc >= -14914 and $asc <= -14631) return "P";
if ($asc >= -14630 and $asc <= -14150) return "Q";
if ($asc >= -14149 and $asc <= -14091) return "R";
if ($asc >= -14090 and $asc <= -13319) return "S";
if ($asc >= -13318 and $asc <= -12839) return "T";
if ($asc >= -12838 and $asc <= -12557) return "W";
if ($asc >= -12556 and $asc <= -11848) return "X";
if ($asc >= -11847 and $asc <= -11056) return "Y";
if ($asc >= -11055 and $asc <= -10247) return "Z";
return $this->FamilyNamesRareWords($asc);
} else if (ord($s) >= 48 and ord($s) <= 57) { // 数字开头
switch (iconv_substr($s, 0, 1, 'utf-8')) {
case 1:
return "Y";
case 2:
return "E";
case 3:
return "S";
case 4:
return "S";
case 5:
return "W";
case 6:
return "L";
case 7:
return "Q";
case 8:
return "B";
case 9:
return "J";
case 0:
return "L";
}
} else if (ord($s) >= 65 and ord($s) <= 90) { //大写英文开头
return substr($s, 0, 1);
} else if (ord($s) >= 97 and ord($s) <= 122) { //小写英文开头
return strtoupper(substr($s, 0, 1));
} else {
return iconv_substr($s0, 0, 1, 'utf-8');
// 中英混合的词语,不适合上面的各种情况,因此直接提取首个字符即可
}
}
/**
* 百家姓中的生僻字
*/
public function FamilyNamesRareWords($asc = '')
{
$rare_arr = array(
-3630 => array('word' => "褚", 'first_char' => 'C'),
-3652 => array('word' => "窦", 'first_char' => 'D'),
-8503 => array('word' => "奚", 'first_char' => 'X'),
-9286 => array('word' => "酆", 'first_char' => 'F'),
-7761 => array('word' => "岑", 'first_char' => 'C'),
-5128 => array('word' => "滕", 'first_char' => 'T'),
-9479 => array('word' => "邬", 'first_char' => 'W'),
-5456 => array('word' => "臧", 'first_char' => 'Z'),
-7223 => array('word' => "闵", 'first_char' => 'M'),
-2877 => array('word' => "裘", 'first_char' => 'Q'),
-6191 => array('word' => "缪", 'first_char' => 'M'),
-5414 => array('word' => "贲", 'first_char' => 'B'),
-4102 => array('word' => "嵇", 'first_char' => 'J'),
-8969 => array('word' => "荀", 'first_char' => 'X'),
-4938 => array('word' => "於", 'first_char' => 'Y'),
-9017 => array('word' => "芮", 'first_char' => 'R'),
-2848 => array('word' => "羿", 'first_char' => 'Y'),
-9477 => array('word' => "邴", 'first_char' => 'B'),
-9485 => array('word' => "隗", 'first_char' => 'K'),
-6731 => array('word' => "宓", 'first_char' => 'M'),
-9299 => array('word' => "郗", 'first_char' => 'X'),
-5905 => array('word' => "栾", 'first_char' => 'L'),
-4393 => array('word' => "钭", 'first_char' => 'T'),
-9300 => array('word' => "郜", 'first_char' => 'G'),
-8706 => array('word' => "蔺", 'first_char' => 'L'),
-3613 => array('word' => "胥", 'first_char' => 'X'),
-8777 => array('word' => "莘", 'first_char' => 'S'),
-6708 => array('word' => "逄", 'first_char' => 'P'),
-9302 => array('word' => "郦", 'first_char' => 'L'),
-5965 => array('word' => "璩", 'first_char' => 'Q'),
-6745 => array('word' => "濮", 'first_char' => 'P'),
-4888 => array('word' => "扈", 'first_char' => 'H'),
-9309 => array('word' => "郏", 'first_char' => 'J'),
-5428 => array('word' => "晏", 'first_char' => 'Y'),
-2849 => array('word' => "暨", 'first_char' => 'J'),
-7206 => array('word' => "阙", 'first_char' => 'Q'),
-4945 => array('word' => "殳", 'first_char' => 'S'),
-9753 => array('word' => "夔", 'first_char' => 'K'),
-10041 => array('word' => "厍", 'first_char' => 'S'),
-5429 => array('word' => "晁", 'first_char' => 'C'),
-2396 => array('word' => "訾", 'first_char' => 'Z'),
-7205 => array('word' => "阚", 'first_char' => 'K'),
-10049 => array('word' => "乜", 'first_char' => 'N'),
-10015 => array('word' => "蒯", 'first_char' => 'K'),
-3133 => array('word' => "竺", 'first_char' => 'Z'),
-6698 => array('word' => "逯", 'first_char' => 'L'),
-9799 => array('word' => "俟", 'first_char' => 'Q'),
-6749 => array('word' => "澹", 'first_char' => 'T'),
-7220 => array('word' => "闾", 'first_char' => 'L'),
-10047 => array('word' => "亓", 'first_char' => 'Q'),
-10005 => array('word' => "仉", 'first_char' => 'Z'),
-3417 => array('word' => "颛", 'first_char' => 'Z'),
-6431 => array('word' => "驷", 'first_char' => 'S'),
-7226 => array('word' => "闫", 'first_char' => 'Y'),
-9293 => array('word' => "鄢", 'first_char' => 'Y'),
-6205 => array('word' => "缑", 'first_char' => 'G'),
-9764 => array('word' => "佘", 'first_char' => 'S'),
-9818 => array('word' => "佴", 'first_char' => 'N'),
-9509 => array('word' => "谯", 'first_char' => 'Q'),
-3122 => array('word' => "笪", 'first_char' => 'D'),
-9823 => array('word' => "佟", 'first_char' => 'T'),
);
if (array_key_exists($asc, $rare_arr) && $rare_arr[$asc]['first_char']) {
return $rare_arr[$asc]['first_char'];
} else {
return null;
}
}
版权声明:本文为withoutfear原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。