PHP獲取漢字首字母並分組排序

2021-08-09 12:38:43 字數 3764 閱讀 1917

php裡獲取第乙個中文首字母並排序,話不多少,上**。

<?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' => 1, 'area_name' => '江蘇'],

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

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

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

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

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

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

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

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

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

];$data = (new character)->groupbyinitials($data, 'area_name');

輸出結果

$data = array(

'a' => array(

0 => array(

'id' => 1,

'area_name' => '安徽',

'initials' => 'a'

)) ,

'f' => array(

0 => array(

'id' => 1,

'area_name' => '福建',

'initials' => 'f'

)) ,

'g' => array(

0 => array(

'id' => 1,

'area_name' => '廣東',

'initials' => 'g'

) ,1 => array(

'id' => 1,

'area_name' => '廣西',

'initials' => 'g'

)) ,

'h' => array(

0 => array(

'id' => 1,

'area_name' => '海南',

'initials' => 'h'

) ,1 => array(

'id' => 1,

'area_name' => '河南',

'initials' => 'h'

) ,2 => array(

'id' => 1,

'area_name' => '湖南',

'initials' => 'h'

) ,3 => array(

'id' => 1,

'area_name' => '湖北',

'initials' => 'h'

)) ,

'j' => array(

0 => array(

'id' => 1,

'area_name' => '江蘇',

'initials' => 'j'

) ,1 => array(

'id' => 1,

'area_name' => '江西',

'initials' => 'j'

)) ,

's' => array(

0 => array(

'id' => 1,

'area_name' => '山東',

'initials' => 's'))

);

PHP獲取漢字首字母並分組排序

寫在前面 經常我們在做專案的時候,會有按首字母排序的需求 比如 美團的城市選擇 正題網上找了各種,不盡人意,於是,自己就寫了乙個,分享給大家。author tech class character data data this sortinitials data return data 按字母排序 ...

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...