php根據漢字首字母分組,利用PHP獲取漢字首字母并且分組排序詳解

前言

本文主要給大家介紹了關于PHP獲取漢字首字母并分組排序的相關內容,因經常我們在做項目的時候,會有按首字母排序的需求 比如: 美團的城市選擇 http://www.meituan.com/index/changecity/initiative app中按字母搜索

正題

網上找了各種,不盡人意,于是,自己就寫了一個,分享給大家。

/**

* @author Tech

*/

class Character

{

/**

* 二維數組根據首字母分組排序

* @param array $data 二維數組

* @param string $targetKey 首字母的鍵名

* @return array 根據首字母關聯的二維數組

*/

public function groupByInitials(array $data, $targetKey = 'name')

{

$data = array_map(function ($item) use ($targetKey) {

return array_merge($item, [

'initials' => $this->getInitials($item[$targetKey]),

]);

}, $data);

$data = $this->sortInitials($data);

return $data;

}

/**

* 按字母排序

* @param array $data

* @return array

*/

public function sortInitials(array $data)

{

$sortData = [];

foreach ($data as $key => $value) {

$sortData[$value['initials']][] = $value;

}

ksort($sortData);

return $sortData;

}

/**

* 獲取首字母

* @param string $str 漢字字符串

* @return string 首字母

*/

public function getInitials($str)

{

if (empty($str)) {return '';}

$fchar = ord($str{0});

if ($fchar >= ord('A') && $fchar <= ord('z')) {

return strtoupper($str{0});

}

$s1 = iconv('UTF-8', 'gb2312', $str);

$s2 = iconv('gb2312', 'UTF-8', $s1);

$s = $s2 == $str ? $s1 : $str;

$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;

if ($asc >= -20319 && $asc <= -20284) {

return 'A';

}

if ($asc >= -20283 && $asc <= -19776) {

return 'B';

}

if ($asc >= -19775 && $asc <= -19219) {

return 'C';

}

if ($asc >= -19218 && $asc <= -18711) {

return 'D';

}

if ($asc >= -18710 && $asc <= -18527) {

return 'E';

}

if ($asc >= -18526 && $asc <= -18240) {

return 'F';

}

if ($asc >= -18239 && $asc <= -17923) {

return 'G';

}

if ($asc >= -17922 && $asc <= -17418) {

return 'H';

}

if ($asc >= -17417 && $asc <= -16475) {

return 'J';

}

if ($asc >= -16474 && $asc <= -16213) {

return 'K';

}

if ($asc >= -16212 && $asc <= -15641) {

return 'L';

}

if ($asc >= -15640 && $asc <= -15166) {

return 'M';

}

if ($asc >= -15165 && $asc <= -14923) {

return 'N';

}

if ($asc >= -14922 && $asc <= -14915) {

return 'O';

}

if ($asc >= -14914 && $asc <= -14631) {

return 'P';

}

if ($asc >= -14630 && $asc <= -14150) {

return 'Q';

}

if ($asc >= -14149 && $asc <= -14091) {

return 'R';

}

if ($asc >= -14090 && $asc <= -13319) {

return 'S';

}

if ($asc >= -13318 && $asc <= -12839) {

return 'T';

}

if ($asc >= -12838 && $asc <= -12557) {

return 'W';

}

if ($asc >= -12556 && $asc <= -11848) {

return 'X';

}

if ($asc >= -11847 && $asc <= -11056) {

return 'Y';

}

if ($asc >= -11055 && $asc <= -10247) {

return 'Z';

}

return null;

}

}

項目中直接引入即可,如果需要命名空間,可以自行添加,下面是我們看看怎么用。

// 按首字母排序

$data = [

['id' => 1, 'area_name' => '山東'],

['id' => 2, 'area_name' => '江蘇'],

['id' => 3, 'area_name' => '安徽'],

['id' => 4, 'area_name' => '福建'],

['id' => 5, 'area_name' => '江西'],

['id' => 6, 'area_name' => '廣東'],

['id' => 7, 'area_name' => '廣西'],

['id' => 8, 'area_name' => '海南'],

['id' => 9, 'area_name' => '河南'],

['id' => 10, 'area_name' => '湖南'],

['id' => 11, 'area_name' => '湖北'],

];

// 初始化,然后調用分組方法

$data = (new Character)->groupByInitials($data, 'area_name');

下面的結果是不是你想要的呢,不論是做app還是網頁,都可以用

$data = array(

'A' => array(

0 => array(

'id' => 3,

'area_name' => '安徽',

'initials' => 'A'

)

) ,

'F' => array(

0 => array(

'id' => 4,

'area_name' => '福建',

'initials' => 'F'

)

) ,

'G' => array(

0 => array(

'id' => 6,

'area_name' => '廣東',

'initials' => 'G'

) ,

1 => array(

'id' => 7,

'area_name' => '廣西',

'initials' => 'G'

)

) ,

'H' => array(

0 => array(

'id' => 8,

'area_name' => '海南',

'initials' => 'H'

) ,

1 => array(

'id' => 9,

'area_name' => '河南',

'initials' => 'H'

) ,

2 => array(

'id' => 10,

'area_name' => '湖南',

'initials' => 'H'

) ,

3 => array(

'id' => 11,

'area_name' => '湖北',

'initials' => 'H'

)

) ,

'J' => array(

0 => array(

'id' => 2,

'area_name' => '江蘇',

'initials' => 'J'

) ,

1 => array(

'id' => 5,

'area_name' => '江西',

'initials' => 'J'

)

) ,

'S' => array(

0 => array(

'id' => 1,

'area_name' => '山東',

'initials' => 'S'

)

)

);

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/537929.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/537929.shtml
英文地址,請注明出處:http://en.pswp.cn/news/537929.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[算法總結] 13 道題搞定 BAT 面試——字符串

本文首發于我的個人博客&#xff1a;尾尾部落 1. KMP 算法 談到字符串問題&#xff0c;不得不提的就是 KMP 算法&#xff0c;它是用來解決字符串查找的問題&#xff0c;可以在一個字符串&#xff08;S&#xff09;中查找一個子串&#xff08;W&#xff09;出現的位置。KMP 算法…

Sqlserver備份存儲過程

查了網上找不到快速備份Sqlserver存儲過程的方法&#xff0c;心里想&#xff0c;如果Sqlserver不自帶這個功能&#xff0c;真是太low了。步驟1&#xff1a;打開存儲過程文件夾步驟2&#xff1a;按 F7 鍵&#xff0c;打開“對象資源管理器詳細信息”窗口步驟3&#xff1a;點擊“…

仿拉鉤app(一)---爬蟲數據準備

工欲善其事必先利其器&#xff0c;準備做一個拉鉤的app&#xff0c;但是沒數據可怎么辦&#xff0c;那就直接扒褲衩去爬吧 一般爬蟲的思路為&#xff1a; 分析頁面結構是否有接口模仿請求&#xff08;解決反爬的各種方式&#xff09;解析數據存儲數據按照以上的思路&#xff0c…

小哼買書JAVA編寫,04_小哼買書

現在來看一個具體的例子“小哼買書”(根據全國青少年信息學奧林匹克聯賽 NOIP2006 普及組第一題改編),來實踐一下 章所學的三種排序算法。Paste_Image.png小哼的學校要建立一個圖書角,老師派小哼去找一些同學做調查,看看同學們都喜歡讀哪些書。小哼讓每個同學寫出一個自己最想讀…

[Err] 22007 - [SQL Server]從 nvarchar 數據類型到 datetime 數據類型的轉換產生一個超出范圍的值。

報錯語句&#xff1a; cast(Replace(Replace(P.DeliverDate,.,-),/,-) as datetime)改為 cast(Replace(Replace(P.DeliverDate,.,-),/,-) as datetime2)使用 datetime2 代替 datetime

linux Postfix + dovecot + extmail + extman + mysql

配置環境&#xff1a;RHEL5.5 i386DNS MX[rootstation40 ~]# host -t MX tianyun.comtianyun.com mail is handled by 10 mail.tianyun.com.[rootstation40 ~]# [rootstation40 ~]# ping mail.tianyun.comPING mail.tianyun.com (192.168.0.2) 56(84) bytes of data.64 bytes f…

php 接口安全解決方案,php接口數據安全解決方案(一)

前言目的&#xff1a;1.實現前后端代碼分離&#xff0c;分布式部署2.利用token替代session實現狀態保持&#xff0c;token是有時效性的滿足退出登錄&#xff0c;token存入redis可以解決不同服務器之間session不同步的問題&#xff0c;滿足分布式部署3.利用sign&#xff0c;前端…

Teamview連接Windows server問題

場景&#xff1a; 服務器在集團總部杭州&#xff0c;網管在集團寧波分公司&#xff0c;連接服務器通過內網遠程桌面。過程&#xff1a; 網管給了tv的賬號&#xff0c;密碼。連接的時候一直連不上去。卡在“正在初始化連接參數”。后來網管不信&#xff0c;遠程桌面了下&#xf…

nginx An attempt was made to access a socket in a way forbidden by its access permissions

在安裝了 sqlserver2008 的win7 與 win2008 上啟動 nginx&#xff0c;綁定80端口&#xff0c;報錯&#xff1a; nginx An attempt was made to access a socket in a way forbidden by its access permissions查了百度&#xff0c;說修改注冊表&#xff0c;但我的電腦上找不到文…

php codesniffer 代碼規范,規范三:PHP_CodeSniffer 輔佐代碼規范

>也可以參考此文&#xff1a;https://www.cnblogs.com/huangbx/p/php_codesniffer.html[TOC]我用的是wamp&#xff0c;環境是php7.0.23# (一)下載 pear打開http://pear.php.net/go-pear.phar&#xff0c;會顯示代碼&#xff0c;不用管他&#xff0c;直接copys復制到本地&…

php的cms是什么意思,phpcms是什么系統

什么是phpcms&#xff1f;Phpcms 是國內領先的網站內容管理系統&#xff0c;同時也是一個開源的PHP開發框架。Phpcms由內容模型、會員、問吧、專題、財務、訂單、廣告、郵件訂閱、 短消息、自定義表單、全站搜索等20多個功能模塊組成&#xff0c;內置新聞、圖片、下載、信息、產…

【python】 time模塊和datetime模塊詳解 【轉】

一、time模塊 time模塊中時間表現的格式主要有三種&#xff1a; a、timestamp時間戳&#xff0c;時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量 b、struct_time時間元組&#xff0c;共有九個元素組。 c、format time 格式化時間&#xff0c;已格式化的結構使時間更…

spring boot Exception in Thread “main” java.lang.classNoFoundException

在客戶測試環境部署&#xff0c;通過打包成jar&#xff0c;使用命令 nohup java -jar /usr/local/tomcat/shirencai/ct-peixun-provider.jar –spring.profiles.activestage > /usr/local/tomcat/shirencai/ct-peixun-provider-temp.txt & 報錯后來排查以為是內存不夠。…

php源碼自動識別文本中的鏈接,自動加載識別文件Auto.php

用于本應用的控制器自動加載類設置&#xff0c;用法如同\CodeIgniter\Config\AutoloadConfig自動加載識別文件:dayrui/App/應用目錄/Config/Auto.php語法格式&#xff1a;<?php // 自動加載識別文件return [/*** 命名空間映射關系*/psr4 > [],/*** 類名映射關系*/classm…

如何識別“答非所問”?使用gensim進行文本相似度計算

在文本處理中&#xff0c;比如商品評論挖掘&#xff0c;有時需要了解每個評論分別和商品的描述之間的相似度&#xff0c;以此衡量評論的客觀性。 評論和商品描述的相似度越高&#xff0c;說明評論的用語比較官方&#xff0c;不帶太多感情色彩&#xff0c;比較注重描述商品的屬性…

防抓包重放php,超簡單最基本的WEB抓包改包重放的方法

【注意&#xff1a;此文章為博主原創文章&#xff01;轉載需注意&#xff0c;請帶原文鏈接&#xff0c;至少也要是txt格式&#xff01;】很多很多剛剛接觸的同事問我如何抓包&#xff0c;如果講用工具可能還涉及什么裝證書&#xff0c;熟悉使用工具等等&#xff0c;特別繁瑣&am…

mysql查詢很慢優化方法1

解決方法&#xff1a; 關聯的字段建索引。 具體分析如下&#xff1a;舉例&#xff1a; 表格&#xff1a;培訓學生表&#xff0c;班級報名表 需求&#xff1a;查詢出學生報了哪些班級 兩表有個關聯字段“CD”&#xff08;學生學號&#xff09;。 視圖sql&#xff1a; SELECTt_px…

ubuntu進行apt-get時候出現Package ssh is not available, but is referred to by another package 錯誤...

今天在ubuntu進行ssh安裝的時候&#xff0c;出現如下錯誤。Reading package lists... Done Building dependency tree... Done Package ssh is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is …

php找出函數定義位置,WordPress如何快速定位PHP函數所在文件位置及代碼行號?

有時候我們需要修改別人源碼里的代碼&#xff0c;卻找不到對應的函數放在了哪兒&#xff0c;就可以用使用本文介紹的辦法&#xff0c;幫你快速定位函數位置。特別是某些寫法不規范的WordPress主題&#xff0c;各種模塊&#xff0c;函數到處放&#xff0c;找半天的那種。那么Wor…

微信公眾號每次調用接口正確或錯誤的返回碼

原文連接&#xff1a;https://blog.csdn.net/pansanday/article/details/65448868 ----------------------------------------- 公眾號每次調用接口時&#xff0c;可能獲得正確或錯誤的返回碼&#xff0c;開發者可以根據返回碼信息調試接口&#xff0c;排查錯誤。 全局返回碼…