Get the first Chinese initial letter in php and sort it

巴扎黑
Release: 2016-11-11 13:09:07
Original
1098 people have browsed it

It’s been a long time since I updated my blog. I still feel that my work needs to be summarized more. Today I will update a question that I think is more successful.
I am doing stored value settlement recently, and the settlement homepage needs to be sorted by the initial letters of the stores A-Z. My data structure was originally like this:

Php code

Array  
(  
    [0] => Array  
        (  
            [sid] => 2885842  
            [recetcstoredpay] => 24000  
            [recetclprinciple] => 23465  
            [paytcstoredpay] => 5455  
            [paytclprinciple] => 34900  
            [sname] => 百宴餐饮---便宜坊烤鸭店  
        )  
  
    [1] => Array  
        (  
            [sid] => 3644191  
            [recetcstoredpay] => 89200  
            [recetclprinciple] => 406930  
            [paytcstoredpay] => 4090  
            [paytclprinciple] => 97800  
            [sname] => 大长秋餐饮中心  
        )  
  
    [2] => Array  
        (  
            [sid] => 5229673  
            [recetcstoredpay] => 26000  
            [recetclprinciple] => 45930  
            [paytcstoredpay] => 24795  
            [paytclprinciple] => 121800  
            [sname] => 大众点评网  
        )  
  
    [3] => Array  
        (  
            [sid] => 3715927  
            [recetcstoredpay] => 13600  
            [recetclprinciple] => 56930  
            [paytcstoredpay] => 5710  
            [paytclprinciple] => 37800  
            [sname] => 江东北路店  
        )  
  
    [4] => Array  
        (  
            [sid] => 3671092  
            [recetcstoredpay] => 1280  
            [recetclprinciple] => 46930  
            [paytcstoredpay] => 128090  
            [paytclprinciple] => 149800  
            [sname] => 金凤区新馆  
        )  
  
    [5] => Array  
        (  
            [sid] => 1858783  
            [recetcstoredpay] => 2040  
            [recetclprinciple] => 4465  
            [paytcstoredpay] => 245  
            [paytclprinciple] => 4900  
            [sname] => 浙江西子宾馆  
        )  
  
    [6] => Array  
        (  
            [sid] => 16832117  
            [recetcstoredpay] => 81600  
            [recetclprinciple] => 470930  
            [paytcstoredpay] => 506090  
            [paytclprinciple] => 8000  
            [sname] => 欢乐谷店  
        )  
  
)
Copy after login

According to the requirements, if I want to sort according to the first letter of the first Chinese character in sname, I first need to write a method to get the first letter:

Php code

/** 
    * 取汉字的第一个字的首字母 
    * @param type $str 
    * @return string|null 
    */  
   public function _getFirstCharter($str){  
if(emptyempty($str)){return '';}  
$fchar=ord($str{0});  
if($fchar>=ord(&#39;A&#39;)&&$fchar<=ord(&#39;z&#39;)) return strtoupper($str{0});  
$s1=iconv(&#39;UTF-8&#39;,&#39;gb2312&#39;,$str);  
$s2=iconv(&#39;gb2312&#39;,&#39;UTF-8&#39;,$s1);  
$s=$s2==$str?$s1:$str;  
$asc=ord($s{0})*256+ord($s{1})-65536;  
if($asc>=-20319&&$asc<=-20284) return &#39;A&#39;;  
if($asc>=-20283&&$asc<=-19776) return &#39;B&#39;;  
if($asc>=-19775&&$asc<=-19219) return &#39;C&#39;;  
if($asc>=-19218&&$asc<=-18711) return &#39;D&#39;;  
if($asc>=-18710&&$asc<=-18527) return &#39;E&#39;;  
if($asc>=-18526&&$asc<=-18240) return &#39;F&#39;;  
if($asc>=-18239&&$asc<=-17923) return &#39;G&#39;;  
if($asc>=-17922&&$asc<=-17418) return &#39;H&#39;;  
if($asc>=-17417&&$asc<=-16475) return &#39;J&#39;;  
if($asc>=-16474&&$asc<=-16213) return &#39;K&#39;;  
if($asc>=-16212&&$asc<=-15641) return &#39;L&#39;;  
if($asc>=-15640&&$asc<=-15166) return &#39;M&#39;;  
if($asc>=-15165&&$asc<=-14923) return &#39;N&#39;;  
if($asc>=-14922&&$asc<=-14915) return &#39;O&#39;;  
if($asc>=-14914&&$asc<=-14631) return &#39;P&#39;;  
if($asc>=-14630&&$asc<=-14150) return &#39;Q&#39;;  
if($asc>=-14149&&$asc<=-14091) return &#39;R&#39;;  
if($asc>=-14090&&$asc<=-13319) return &#39;S&#39;;  
if($asc>=-13318&&$asc<=-12839) return &#39;T&#39;;  
if($asc>=-12838&&$asc<=-12557) return &#39;W&#39;;  
if($asc>=-12556&&$asc<=-11848) return &#39;X&#39;;  
if($asc>=-11847&&$asc<=-11056) return &#39;Y&#39;;  
if($asc>=-11055&&$asc<=-10247) return &#39;Z&#39;;  
return null;  
   }
Copy after login

Then the next step is to sort this two-dimensional data. I thought about it for a long time, and then I came up with a plan. First, call this method to get the first letter in the loop, and then use this letter as the key. Because there is a method of sorting according to the key in PHP, so my code is written like this:

Php code

//门店名称  
           $shopData = $this->_shopNamesArray;    
             
           //根据门店名称第一个汉字的首字母正序排序  
           $settles = $result[&#39;data&#39;];  
        
           $settlesRes = array();  
           foreach ($settles as $sett) {  
               $sname = $shopData[$sett[&#39;sid&#39;]];  
               $sett[&#39;sname&#39;] = $sname;    
               $snameFirstChar = $this->_getFirstCharter($sname); //取出门店的第一个汉字的首字母  
               $settlesRes[$snameFirstChar] = $sett;//以这个首字母作为key  
           }              
           ksort($settlesRes); //对数据进行ksort排序,以key的值升序排序
Copy after login

                                                1

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template