php中文排序

按首字母排序(获取首字母)

首字母排序

推荐使用sql

SQL:(利用的是数据库的默认编码排序原理)

https://blog.csdn.net/xiaojiejie_baby/article/details/90670304

1 Hospital::orderBy(DB::raw(‘convert(‘hospital_name‘ using gbk)‘))->get();

 

方法:

  1 class CharacterServices
  2 {
  3     /**
  4      * 二维数组根据首字母分组排序
  5      * @param  array  $data      二维数组
  6      * @param  string $targetKey 首字母的键名
  7      * @return array             根据首字母关联的二维数组
  8      */
  9     public static  function groupByInitials(array $data,$targetKey = ‘name‘)
 10     {
 11         $data = array_map(function ($item) use ($targetKey) {//数组加key
 12             return array_merge($item,[//合并数组
 13                 ‘initials‘ => self::getInitials($item[$targetKey]),//获取首字母
 14             ]);
 15         },$data);
 16 
 17         $data = self::sortInitials($data);//排序
 18         return $data;
 19     }
 20 
 21     /**
 22      * 按字母排序
 23      * @param  array  $data
 24      * @return array
 25      */
 26     public static function sortInitials(array $data)
 27     {
 28         $sortData = [];
 29         foreach ($data as $key => $value){
 30             $sortData[$value[‘initials‘]][] = $value;
 31         }
 32         ksort($sortData);//关联键名,升序排列
 33         return $sortData;
 34     }
 35 
 36     /**
 37      * 获取首字母
 38      * @param  string $str 汉字字符串
 39      * @return string 首字母
 40      */
 41     public static function getInitials($str)
 42     {
 43         if (empty($str)) {return ‘‘;}
 44         $fchar = ord($str{0});
 45         if ($fchar >= ord(‘A‘) && $fchar <= ord(‘z‘)) {//输出ASCII码
 46             return strtoupper($str{0});//转大写
 47         }
 48         $s1  = iconv(‘UTF-8‘,‘gb2312‘,$str);//转码
 49         $s2  = iconv(‘gb2312‘,‘UTF-8‘,$s1);//转码
 50         $s   = $s2 == $str ? $s1 : $str;
 51         $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;//根据GB2312码表拼音表范围
 52         if ($asc >= -20319 && $asc <= -20284) {
 53             return ‘A‘;
 54         }
 55 
 56         if ($asc >= -20283 && $asc <= -19776) {
 57             return ‘B‘;
 58         }
 59 
 60         if ($asc >= -19775 && $asc <= -19219) {
 61             return ‘C‘;
 62         }
 63 
 64         if ($asc >= -19218 && $asc <= -18711) {
 65             return ‘D‘;
 66         }
 67 
 68         if ($asc >= -18710 && $asc <= -18527) {
 69             return ‘E‘;
 70         }
 71 
 72         if ($asc >= -18526 && $asc <= -18240) {
 73             return ‘F‘;
 74         }
 75 
 76         if ($asc >= -18239 && $asc <= -17923) {
 77             return ‘G‘;
 78         }
 79 
 80         if ($asc >= -17922 && $asc <= -17418) {
 81             return ‘H‘;
 82         }
 83 
 84         if ($asc >= -17417 && $asc <= -16475) {
 85             return ‘J‘;
 86         }
 87 
 88         if ($asc >= -16474 && $asc <= -16213) {
 89             return ‘K‘;
 90         }
 91 
 92         if ($asc >= -16212 && $asc <= -15641) {
 93             return ‘L‘;
 94         }
 95 
 96         if ($asc >= -15640 && $asc <= -15166) {
 97             return ‘M‘;
 98         }
 99 
100         if ($asc >= -15165 && $asc <= -14923) {
101             return ‘N‘;
102         }
103 
104         if ($asc >= -14922 && $asc <= -14915) {
105             return ‘O‘;
106         }
107 
108         if ($asc >= -14914 && $asc <= -14631) {
109             return ‘P‘;
110         }
111 
112         if ($asc >= -14630 && $asc <= -14150) {
113             return ‘Q‘;
114         }
115 
116         if ($asc >= -14149 && $asc <= -14091) {
117             return ‘R‘;
118         }
119 
120         if ($asc >= -14090 && $asc <= -13319) {
121             return ‘S‘;
122         }
123 
124         if ($asc >= -13318 && $asc <= -12839) {
125             return ‘T‘;
126         }
127 
128         if ($asc >= -12838 && $asc <= -12557) {
129             return ‘W‘;
130         }
131 
132         if ($asc >= -12556 && $asc <= -11848) {
133             return ‘X‘;
134         }
135 
136         if ($asc >= -11847 && $asc <= -11056) {
137             return ‘Y‘;
138         }
139 
140         if ($asc >= -11055 && $asc <= -10247) {
141             return ‘Z‘;
142         }
143 
144         return null;
145     }
146 
147 }

使用方法示例

 1 //model层
 2 static $Character = [
 3     ‘‘=>0,‘A‘ =>1,‘B‘ =>2,‘C‘ =>3,‘D‘ =>4,‘E‘ =>5,‘F‘ =>6,‘G‘ =>7,‘H‘ =>8,‘I‘ =>9, 4     ‘J‘ =>10,‘K‘ =>11,‘L‘ =>12,‘M‘ =>13,‘N‘ =>14,‘O‘ =>15,‘P‘ =>16,‘Q‘ =>17,‘R‘ =>18, 5     ‘S‘ =>19,‘T‘ =>20,‘U‘ =>21,‘V‘ =>22,‘W‘ =>23,‘X‘ =>24,‘Y‘ =>25,‘Z‘ =>26
 6 ];
 7 
 8 //控制器层
 9 $oSorting = CharacterServices::groupByInitials($aDiseaseList,‘wiki_name‘);
10 foreach ($oSorting as $key => $value) {
11     $arr = [
12         ‘id‘   => WikiList::$Character[$key],13         ‘name‘ => $key,14         ‘list‘ => $value,15     ];
16     $aNew[] = $arr;
17 }

相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...