获取通讯库中外文首字排序(加强篇)

  上一篇用到的获取首字母并不全面,对于通讯录中的一些生僻的姓都是返回NULL的,体验极差。于是在composer库中找到了基于词库的中文转拼音优质解决方案,同时附上一位大神的使用手册更准确的 PHP 汉字转拼音解决方案,于是上一个接口也就变成下面的
<?php
/**
 * 
 * @authors ZL 
 * @email 987968469@qq.com
 * @date    2017-12-14 11:23:35
 */
namespace app\contact\model;

use think\Model;
use think\Db;
use contact\Contact as ContactName;
use Overtrue\Pinyin\Pinyin;

class Contact extends Model {
    /**
     * 河长通讯录
     * @return bool|\think\response\Json
     */   
     public function getContactList(){
        $data = Db::name('admin_user')->alias('au')
            ->join('river_level rl','au.level = rl.id','left')
            ->field(['au.id userId','au.user_name userName','au.mobile','rl.level_name levelName','au.department','au.position','au.department_tel'])
            ->where("au.status",1)
            ->order('CONVERT(au.user_name USING gbk)')
            ->select()
            ;//Dump($data);die;
          //旧方法获取首字母,有些字获取不到
        // $firstName = new ContactName;
          // 小内存型
          $pinyin = new Pinyin(); // 默认
          // 内存型
          // $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
          // I/O型
          // $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');
        $aaa = $bbb = $ccc = array();
        foreach($data as $k => $v){
               $firstPinYin = $pinyin->name(mb_substr(trim($v['userName']), 0,1));
               $firstCharacter = strtoupper(substr($firstPinYin[0], 0, 1)) ;
            //$firstCharacter = $firstName->_getFirstCharacter($v['userName']);
            if(in_array($firstCharacter,$bbb)){
                array_push($ccc,$v);
                $bbb['contact'] = $ccc;
            }else{
                array_push($aaa,$bbb);
                $bbb['firstPY'] = $firstCharacter;
                $ccc = array();
                array_push($ccc,$v);
                $bbb['contact'] = $ccc;
            }               
        }
        array_push($aaa,$bbb);
        array_shift($aaa);
        return $aaa;        
     }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,636评论 25 708
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,264评论 3 119
  • 生病了,工作十分难受,晚上回家就躺在床上,12点才睡
    兆之阅读 72评论 0 0
  • 体验入:在企业中如何建立自主成长系统,给什么不如给目标、给未来、给空间,我的地盘我做主、我的事情我来做、我的责任我...
    熊毅滨1349阅读 180评论 0 0
  • 上进而又没有野心的男生真的很难得 见过很多不上进的男孩子,做朋友其乐融融但若是作为另一半考虑的话大概时间久了我会有...
    小十八碎碎念阅读 192评论 1 0

友情链接更多精彩内容