PHP sorted in Chinese first letter

Source: Internet
Author: User
Tags ord

1> Many PHP tool classes on the network can convert Chinese characters to pinyin;

2> to sort the pinyin

The other is similar to the MySQL transcoding method:

1foreach($array  as $key=$value)  2     {  3$new _array[$key] =Iconv(' UTF-8 ', ' GBK ',$value); 4} 1foreach($array  as $key=$value)  2     {  3$new _array[$key] =Iconv(' UTF-8 ', ' GBK ',$value); 4     }  5Asort($new _array); 6foreach($new _array  as $key=$value)  7     {  8$array[$key] =Iconv(' GBK ', ' UTF-8 ',$value); 9     } 5Asort($new _array); 6foreach($new _array  as $key=$value)  7     {  8$array[$key] =Iconv(' GBK ', ' UTF-8 ',$value); 9}

Consolidated Case List

//Sort by Chinese first letterfunctionOrderbyname ($userName,$order= ' ASC ',$key= ' Jlname '){     foreach($userName  as $name){        if(Is_array($name))$char= Getfirstchar ($name[$key]); ElseIf(is_string($name))$char= Getfirstchar ($name); $nameArray=Array();//Pair the name with the first letter of the last name and the relative first letter key        if(Count($charArray[$char])!=0)$nameArray=$charArray[$char]; Array_push($nameArray,$name); $charArray[$char] =$nameArray; }     if(Strtolower($order) = = ' ASC ')Ksort($charArray);ElseIf(Strtolower($order) = = ' desc ')Krsort($charArray); $newarr=Array(); $i=0; foreach($charArray  as $ck=$CV){        if(Is_array($CV)){            foreach($CV  as $CCK=$CCV){                $newarr[$i++] =$CCV; }           }Else{            $newarr[$i++]=$CV; }    }    /*Echo ':<br> before alphabetical order ';      Print_r ($charArray);      Sort echo ' alphabetically by:<br> ' according to key value pairs; Print_r ($charArray);*/      return $newarr; }//get the first letter in ChinesefunctionGetfirstchar ($s){      $s 0= Mb_substr ($s, 0,1, ' utf-8 ');//get the surname of the first name    $s=Iconv(' UTF-8 ', ' GBK ',$s 0);//convert UTF-8 to GB2312 encoding    if(Ord($s 0) {>128) {//Chinese characters do not start with U or v    $ASC=Ord($s{0}) *256+Ord($s{1}) -65536; if($ASC>=-20319 and$ASC<=-20284)returnA; if($ASC>=-20283 and$ASC<=-19776)returnB; if($ASC>=-19775 and$ASC<=-19219)returnC; if($ASC>=-19218 and$ASC<=-18711)returnD; if($ASC>=-18710 and$ASC<=-18527)returnE; if($ASC>=-18526 and$ASC<=-18240)returnF; if($ASC>=-18239 and$ASC<=-17760)returnG; if($ASC>=-17759 and$ASC<=-17248)returnH; if($ASC>=-17247 and$ASC<=-17418)return"I"; if($ASC>=-17417 and$ASC<=-16475)returnJ; if($ASC>=-16474 and$ASC<=-16213)returnK; if($ASC>=-16212 and$ASC<=-15641)returnL; if($ASC>=-15640 and$ASC<=-15166)returnM; if($ASC>=-15165 and$ASC<=-14923)returnN; if($ASC>=-14922 and$ASC<=-14915)returnO; if($ASC>=-14914 and$ASC<=-14631)returnP; if($ASC>=-14630 and$ASC&LT;=-14150)return"Q"; if($ASC>=-14149 and$ASC<=-14091)returnR; if($ASC>=-14090 and$ASC<=-13319)returnS; if($ASC>=-13318 and$ASC<=-12839)returnT; if($ASC>=-12838 and$ASC<=-12557)returnW; if($ASC>=-12556 and$ASC<=-11848)returnX; if($ASC>=-11847 and$ASC<=-11056)returnY; if($ASC>=-11055 and$ASC<=-10247)returnZ; }ElseIf(Ord($s) >=48 andOrd($s) {<=57) {//Number Start        Switch(Iconv_substr($s, 0,1, ' utf-8 ')){               Case1:returnY;  Case2:returnE;  Case3:returnS;  Case4:returnS;  Case5:returnW;  Case6:returnL;  Case7:return"Q";  Case8:returnB;  Case9:returnJ;  Case0:returnL; }                     }Else if(Ord($s) >=65 andOrd($s) {<=90) {//Capital English Start        return substr($s, 0,1); }Else if(Ord($s) >=97 andOrd($s) {<=122) {//lowercase English Opening        return Strtoupper(substr($s, 0,1)); }Else{          return Iconv_substr($s 0, 0,1, ' utf-8 ');//Chinese and English mixed words to extract the first character can be    }  }

MySQL implementation

If the current database encoding is Utf-8, then transcode to GBK,GBK default Chinese characters are sorted by pinyin:

From USER ASC 

If the current encoding is GBK:

Asc

PHP sorted in Chinese first letter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.