In the project encountered the need to put the game in alphabetical order, so Baidu to a formatted initial letter method.
/** * @name php get Chinese characters Pinyin first letter * @param $str * @return null|string*/ Public functionGetfirstcharter ($str) { if(Empty($str)) { return‘‘; } $fchar=Ord($str{0}); if($fchar>=Ord(' A ') &&$fchar<=Ord(' Z ')) { return Strtoupper($str{0}); } $s 1=Iconv(' UTF-8 ', ' gb2312 ',$str); $s 2=Iconv(' gb2312 ', ' UTF-8 ',$s 1); $s=$s 2==$str?$s 1:$str; $ASC=Ord($s{0}) * 256 +Ord($s{1})-65536; if($ASC>= -20319 &&$ASC<=-20284) { returnA; } if($ASC>= -20283 &&$ASC<=-19776) { returnB; } if($ASC>= -19775 &&$ASC<=-19219) { returnC; } if($ASC>= -19218 &&$ASC<=-18711) { returnD; } if($ASC>= -18710 &&$ASC<=-18527) { returnE; } if($ASC>= -18526 &&$ASC<=-18240) { returnF; } if($ASC>= -18239 &&$ASC<=-17923) { returnG; } if($ASC>= -17922 &&$ASC<=-17418) { returnH; } if($ASC>= -17417 &&$ASC<=-16475) { returnJ; } if($ASC>= -16474 &&$ASC<=-16213) { returnK; } if($ASC>= -16212 &&$ASC<=-15641) { returnL; } if($ASC>= -15640 &&$ASC<=-15166) { returnM; } if($ASC>= -15165 &&$ASC<=-14923) { returnN; } if($ASC>= -14922 &&$ASC<=-14915) { returnO; } if($ASC>= -14914 &&$ASC<=-14631) { returnP; } if($ASC>= -14630 &&$ASC<=-14150) { return' Q '; } if($ASC>= -14149 &&$ASC<=-14091) { returnR; } if($ASC>= -14090 &&$ASC<=-13319) { returnS; } if($ASC>= -13318 &&$ASC<=-12839) { returnT; } if($ASC>= -12838 &&$ASC<=-12557) { returnW; } if($ASC>= -12556 &&$ASC<=-11848) { returnX; } if($ASC>= -11847 &&$ASC<=-11056) { returnY; } if($ASC>= -11055 &&$ASC<=-10247) { returnZ; } returnOther; }
The above filter can only be filtered to a portion, because of the use of Chinese as a key can have a variety of problems, so I have to re-sort filtering.
/** Format Game area * Author LHC * DateTime 2017-06-04t15:48:59+0800*/ Public functionFormatgamecharter ($gameData) { if(Empty($gameData)) { return $gameData; } $KEYARR= []; $gameZone= []; foreach($gameData as $game) { $key=$this->getfirstcharter ($game[' Name ']); if(In_array($key,$KEYARR)) { $gameZone[$key[' key '] =$key; $gameZone[$key[' list '] [] = [ ' GameId ' =intval($game[' ID ']), ' appId ' =intval($game[' AppId ']), ' gamename ' =$game[' Name '], ]; } Else { $KEYARR[] =$key; } } Array_multisort($gameZone); $gameZone=array_values($gameZone); return $gameZone; }
There is also a way to first put 26 letters with the other first combination good one array, and then iterate through the inserted array inside, this is not a bad place I think if a letter under the game is empty, the letter under the empty bad look, of course, to see the actual needs, if you think you can solve your problem, you can do that.
PHP get Chinese characters Pinyin first letter