寫在前面
經常我們在做專案的時候,會有按首字母排序的需求
比如:美團的城市選擇
正題網上找了各種,不盡人意,於是,自己就寫了乙個,分享給大家。
<?php
/** * @author tech
*/class character
, $data);
$data = $this->sortinitials($data);
return $data;
}/**
* 按字母排序
* @param array $data
* @return array
*/public function sortinitials(array $data)
ksort($sortdata);
return $sortdata;
}/**
* 獲取首字母
* @param string $str 漢字字串
* @return string 首字母
*/public function getinitials($str)
$fchar = ord($str);
if ($fchar >= ord('a') && $fchar <= ord('z')) );
}$s1 = iconv('utf-8', 'gb2312', $str);
$s2 = iconv('gb2312', 'utf-8', $s1);
$s = $s2 == $str ? $s1 : $str;
$asc = ord($s) * 256 + ord($s) - 65536;
if ($asc >= -20319 && $asc <= -20284)
if ($asc >= -20283 && $asc <= -19776)
if ($asc >= -19775 && $asc <= -19219)
if ($asc >= -19218 && $asc <= -18711)
if ($asc >= -18710 && $asc <= -18527)
if ($asc >= -18526 && $asc <= -18240)
if ($asc >= -18239 && $asc <= -17923)
if ($asc >= -17922 && $asc <= -17418)
if ($asc >= -17417 && $asc <= -16475)
if ($asc >= -16474 && $asc <= -16213)
if ($asc >= -16212 && $asc <= -15641)
if ($asc >= -15640 && $asc <= -15166)
if ($asc >= -15165 && $asc <= -14923)
if ($asc >= -14922 && $asc <= -14915)
if ($asc >= -14914 && $asc <= -14631)
if ($asc >= -14630 && $asc <= -14150)
if ($asc >= -14149 && $asc <= -14091)
if ($asc >= -14090 && $asc <= -13319)
if ($asc >= -13318 && $asc <= -12839)
if ($asc >= -12838 && $asc <= -12557)
if ($asc >= -12556 && $asc <= -11848)
if ($asc >= -11847 && $asc <= -11056)
if ($asc >= -11055 && $asc <= -10247)
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');
$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'))
);
原文
PHP獲取漢字首字母並分組排序
php裡獲取第乙個中文首字母並排序,話不多少,上 author tech class character data data this sortinitials data return data 按字母排序 param array data return array public function ...
PHP獲取漢字首字母並分組排序
author tech class character data data this sortinitials data return data 按字母排序 param array data return array public function sortinitials array data k...
java獲取漢字首字母
public class chinesefcutil 二十六個字母區間對應二十七個端點 gb2312碼漢字區間十進位制表示 private static int table new int 27 對應首字母區間表 private static char initialtable 初始化 static...