PHP gets the first Chinese initials and sorts the method

Source: Internet
Author: User
Tags ord sorts
This article mainly introduces the PHP implementation to get the first Chinese first letter and sorting methods, involving the PHP array traversal, encoding conversion and array sorting related operation skills, the need for friends can refer to the following

Specific as follows:

Recently in the storage value settlement, the need to settle the home page needs to be sorted by store's first letter A-Z. My data structure is the original:

Array ([0] = = Array ([SID] = 2885842 [Recetcstoredpay] = 24000 [Recetclprinciple] = 234 [Paytcstoredpay] = 5455 [Paytclprinciple] = 34900 [sname] + yum restaurant---cheap square roast duck restaurant) [1] = + Ar Ray ([SID] = 3644191 [Recetcstoredpay] = 89200 [Recetclprinciple] = 406930 [Paytcstore  Dpay] = 4090 [Paytclprinciple] = 97800 [sname] and big long Autumn Dining center) [2] = = Array ([SID] = 5229673 [Recetcstoredpay] = 26000 [Recetclprinciple] = 45930 [Paytcstoredpay] = 24795 [pa Ytclprinciple] = 121800 [sname] + Volkswagen reviews Web) [3] = = Array ([sid] = 3715927 [Recetcstore      Dpay] = 13600 [Recetclprinciple] = 56930 [Paytcstoredpay] = 5710 [paytclprinciple] + 37800 [Sname] = jiangdong North Road store) [4] = = Array ([sid] = 3671092 [Recetcstoredpay] = [Recet Clprinciple] = 46930      [Paytcstoredpay] = 128090 [Paytclprinciple] = 149800 [sname] = Golden Phoenix District new Building) [5] = = Array ([SID] = 1858783 [Recetcstoredpay] = 2040 [recetclprinciple] = 4465 [Paytcstoredpay] =&gt ;      245 [Paytclprinciple] = 4900 [sname] = Zhejiang xizi hotel) [6] = = Array ([sid] = 16832117 [Recetcstoredpay] = 81600 [Recetclprinciple] = 470930 [Paytcstoredpay] = 506090 [PAYTCLPRINCIPL E] = 8000 [sname] = Happy Valley Store)

According to the demand, according to the first character of sname, then we need to write a method of first letter:

/*** the first letter of a Chinese character * @param type $STR * @return string|null*/public function _getfirstcharter ($str) {if (Emptyempty ($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) 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 the two-dimensional data. I thought for a long time, then thought of the scheme, first call the first letter in the loop method, and then use this letter as key, because PHP has a method based on key, so my code is written so that it is done:

Store name $shopdata = $this->_shopnamesarray;//based on the name of the first Chinese character in the store $settles = $result [' data ']; $settlesRes = Array (); foreach ($settles as $sett) {    $sname = $shopData [$sett [' Sid ']];    $sett [' sname '] = $sname;    $snameFirstChar = $this->_getfirstcharter ($sname); Remove the first character of the store    $settlesRes [$snameFirstChar] = $sett;//With this initials as Key}ksort ($settlesRes); Sort the data in ascending order with the value of key Ksort

Print out this data first to see the effect:

Array ([B] = = Array ([SID] = 2885842 [Recetcstoredpay] = 24000 [Recetclprinciple] = 234 [Paytcstoredpay] = 5455 [Paytclprinciple] = 34900 [sname] + yum restaurant---cheap square roast duck restaurant) [D] = Ar Ray ([SID] = 5229673 [Recetcstoredpay] = 26000 [Recetclprinciple] = 45930 [paytcstored Pay] = 24795 [paytclprinciple] + 121800 [sname] + Volkswagen reviews net) [H] = = Array ([SID] = 16832117 [Recetcstoredpay] = 81600 [Recetclprinciple] = 470930 [Paytcstoredpay] = 506090 [ Paytclprinciple] = 8000 [sname] = Happy Valley Store) [J] = = Array ([sid] = 3671092 [recetcstored       Pay] [recetclprinciple] = 46930 [Paytcstoredpay] = 128090 [Paytclprinciple] = 149800 [Sname] = Jinfeng District new building) [Z] = = Array ([sid] = 1858783 [Recetcstoredpay] = 2040 [rece Tclprinciple] = 4465      [Paytcstoredpay] = 245 [Paytclprinciple] = 4900 [sname] = Zhejiang Xizi hotel)) 

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.