按姓名的拼音首字母排序 --第三方Pinyin

时间:2021-12-30 07:57:04
 1 (逻辑过程省略)例子:
 2 
 3 $name_datas    //姓名数据,类型array
 4 
 5 $pinyin = app(Pinyin::class);  //加载第三方拼音类
 6 foreach ($name_datas as $key => $value) 
 7 {
 8    $letter= $pinyin->abbr($value['name']);    //获取姓名拼音首个字母
 9  
10   //TODO:数据根据姓名字母分组分块
11       ...
12       ...
13       ...
14   //TODO:数据逻辑以及数据返回结构出来等
15 }
16   
17 $result = collect($data)->sortBy('letter');  //根据字母排序
18 return $result->values();

 备注:composer require (composer加载 Pinyin第三方)

 "require": {
        "php": "^7.1.3",
        "dingo/api": "2.0.0-alpha1",
        "fideloper/proxy": "^4.0",
        "hootlex/laravel-moderation": "^1.1",
        "kalnoy/nestedset": "^4.3",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0",
        "latrell/rongcloud": "dev-master",
        "overtrue/pinyin": "^4.0",          //Pinyin第三方扩展类
        "predis/predis": "^1.1",
        "sentry/sentry-laravel": "^0.9.1",
        "spatie/laravel-permission": "^2.33"
    },