很久没来更新博客了,还是觉得工作需要多总结的好。今天来更新一个自己认为比较有成就的问题吧。
最近在做储值结算,需求里结算首页需要按门店的首字母A-Z排序。我的数据结构原本是这样的:
Php代码
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的第一个汉字首字母排序,那么就先需要写一个取首字母的方法:
Php代码
/** * 取汉字的第一个字的首字母 * @param type $str * @return string|null */ public function _getFirstCharter($str){ if(emptyempty($str)){return '';} $fchar=ord($str{0}); if($fchar>=ord('A')&&$fchar=-20319&&$asc=-20283&&$asc=-19775&&$asc=-19218&&$asc=-18710&&$asc=-18526&&$asc=-18239&&$asc=-17922&&$asc=-17417&&$asc=-16474&&$asc=-16212&&$asc=-15640&&$asc=-15165&&$asc=-14922&&$asc=-14914&&$asc=-14630&&$asc=-14149&&$asc=-14090&&$asc=-13318&&$asc=-12838&&$asc=-12556&&$asc=-11847&&$asc=-11055&&$asc然后下一步,要对这个二维数据排序。我思考了很久,后来想到了方案,先在循环里调用这个取首字母的方法,然后以这个字母作为key,因为php里有根据key排序的方法,所以我的代码写成这样就搞定了:
Php代码
//门店名称 $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出来看效果:
Php代码
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] => 浙江西子宾馆 ) )完全正确,搞定了~
已抢26697个
抢已抢14144个
抢已抢7099个
抢已抢8614个
抢已抢17589个
抢已抢7069个
抢