Get the first Chinese initials in php and sort them

https://blog.csdn.net/zifengye520/article/details/50204629

https://blog.csdn.net/nalw2012/article/details/49946917

It's been a long time since I updated the blog, and I still feel that the work needs to be summarized more. Today, let's update a question that I think is more successful.
Recently, I am doing stored value settlement, and the homepage of the settlement in demand needs to be sorted by the first letter AZ of the store. My data structure originally looked like this:

Php code  
Array  
(  
    [0] => Array  
        (  
            [sid] => 2885842  
            [recetcstoredpay] => 24000  
            [recetclprinciple] => 23465  
            [paytcstoredpay] => 5455  
            [paytclprinciple] => 34900  
            [sname] => Baiyan Catering---Bianyifang Roast Duck Restaurant  
        )  
  
    [1] => Array  
        (  
            [sid] => 3644191  
            [recetcstoredpay] => 89200  
            [recetclprinciple] => 406930  
            [paytcstoredpay] => 4090  
            [paytclprinciple] => 97800  
            [sname] => Dachangqiu Dining Center  
        )  
  
    [2] => Array  
        (  
            [sid] => 5229673  
            [recetcstoredpay] => 26000  
            [recetclprinciple] => 45930  
            [paytcstoredpay] => 24795  
            [paytclprinciple] => 121800  
            [sname] => Dianping.com  
        )  
  
    [3] => Array  
        (  
            [sid] => 3715927  
            [recetcstoredpay] => 13600  
            [recetclprinciple] => 56930  
            [paytcstoredpay] => 5710  
            [paytclprinciple] => 37800  
            [sname] => Jiangdong North Road Store  
        )  
  
    [4] => Array  
        (  
            [sid] => 3671092  
            [recetcstoredpay] => 1280  
            [recetclprinciple] => 46930  
            [paytcstoredpay] => 128090  
            [paytclprinciple] => 149800  
            [sname] => Jinfeng District New Building  
        )  
  
    [5] => Array  
        (  
            [sid] => 1858783  
            [recetcstoredpay] => 2040  
            [recetclprinciple] => 4465  
            [paytcstoredpay] => 245  
            [paytclprinciple] => 4900  
            [sname] => Zhejiang Xizi Hotel  
        )  
  
    [6] => Array  
        (  
            [sid] => 16832117  
            [recetcstoredpay] => 81600  
            [recetclprinciple] => 470930  
            [paytcstoredpay] => 506090  
            [paytclprinciple] => 8000  
            [sname] => Happy Valley Store  
        )  
  
)  

 According to the requirements, to sort according to the first letter of the first Chinese character of sname, then you need to write a method to get the first letter:

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

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

Php code
// store name  
           $shopData = $this->_shopNamesArray;    
             
           // Sort according to the positive order of the first letter of the first Chinese character of the store name  
           $settles = $result['data'];  
        
           $settlesRes = array();  
           foreach ($settles as $sett) {  
               $sname = $shopData[$sett['sid']];  
               $ sett ['sname'] = $ sname;    
               $snameFirstChar = $this->_getFirstCharter($sname); //Take out the first letter of the first Chinese character in the store  
               $settlesRes[$snameFirstChar] = $sett;//Use this first letter as the key  
           }              
           ksort($settlesRes); //Ksort the data in ascending order by the value of the key  

First print out these data to see the effect:

Array  
(  
    [B] => Array  
        (  
            [sid] => 2885842  
            [recetcstoredpay] => 24000  
            [recetclprinciple] => 23465  
            [paytcstoredpay] => 5455  
            [paytclprinciple] => 34900  
            [sname] => Baiyan Catering---Bianyifang Roast Duck Restaurant  
        )  
  
    [D] => Array  
        (  
            [sid] => 5229673  
            [recetcstoredpay] => 26000  
            [recetclprinciple] => 45930  
            [paytcstoredpay] => 24795  
            [paytclprinciple] => 121800  
            [sname] => Dianping.com  
        )  
  
    [H] => Array  
        (  
            [sid] => 16832117  
            [recetcstoredpay] => 81600  
            [recetclprinciple] => 470930  
            [paytcstoredpay] => 506090  
            [paytclprinciple] => 8000  
            [sname] => Happy Valley Store  
        )  
  
    [J] => Array  
        (  
            [sid] => 3671092  
            [recetcstoredpay] => 1280  
            [recetclprinciple] => 46930  
            [paytcstoredpay] => 128090  
            [paytclprinciple] => 149800  
            [sname] => Jinfeng District New Building  
        )  
  
    [Z] => Array  
        (  
            [sid] => 1858783  
            [recetcstoredpay] => 2040  
            [recetclprinciple] => 4465  
            [paytcstoredpay] => 245  
            [paytclprinciple] => 4900  
            [sname] => Zhejiang Xizi Hotel  
        )  
  
)  

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324991928&siteId=291194637