当前位置:首页 > PHP教程 > PHP高级教程

PHP实现获取第一个中文首字母并进行排序的方法

本文实例讲述了php实现获取第一个中文首字母并进行排序的方法。分享给大家供大家参考,具体如下:

最近在做储值结算,需求里结算首页需要按门店的首字母a-z排序。我的数据结构原本是这样的:

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] => 欢乐谷店 ) )

根据需求,要根据sname的第一个汉字首字母排序,那么就先需要写一个取首字母的方法:

/** * 取汉字的第一个字的首字母 * @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}); $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; }

然后下一步,要对这个二维数据排序。我思考了很久,后来想到了方案,先在循环里调用这个取首字母的方法,然后以这个字母作为key,因为php里有根据key排序的方法,所以我的代码写成这样就搞定了:

//门店名称 $shopdata = $this->_shopnamesarray; //根据门店名称第一个汉字的首字母正序排序 $settles = $result['data']; $settlesres = array(); foreach ($settles as $sett) { $sname = $shopdata[$sett['sid']]; $sett['sname'] = $sname; $snamefirstchar = $this->_getfirstcharter($sname); //取出门店的第一个汉字的首字母 $settlesres[$snamefirstchar] = $sett;//以这个首字母作为key } ksort($settlesres); //对数据进行ksort排序,以key的值升序排序

先把这些数据print出来看效果:

array ( [b] => array ( [sid] => 2885842 [recetcstoredpay] => 24000 [recetclprinciple] => 23465 [paytcstoredpay] => 5455 [paytclprinciple] => 34900 [sname] => 百宴餐饮---便宜坊烤鸭店 ) [d] => array ( [sid] => 5229673 [recetcstoredpay] => 26000 [recetclprinciple] => 45930 [paytcstoredpay] => 24795 [paytclprinciple] => 121800 [sname] => 大众点评网 ) [h] => array ( [sid] => 16832117 [recetcstoredpay] => 81600 [recetclprinciple] => 470930 [paytcstoredpay] => 506090 [paytclprinciple] => 8000 [sname] => 欢乐谷店 ) [j] => array ( [sid] => 3671092 [recetcstoredpay] => 1280 [recetclprinciple] => 46930 [paytcstoredpay] => 128090 [paytclprinciple] => 149800 [sname] => 金凤区新馆 ) [z] => array ( [sid] => 1858783 [recetcstoredpay] => 2040 [recetclprinciple] => 4465 [paytcstoredpay] => 245 [paytclprinciple] => 4900 [sname] => 浙江西子宾馆 ) )


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!