将中文都转为ascii值之后进行排序
protected function strtoascii($str){ $str=mb_convert_encoding($str,'GB2312'); $change_after=''; for($i=0;$i<strlen($str);$i++){ $temp_str=dechex(ord($str[$i])); $change_after.=$temp_str[1].$temp_str[0]; } return strtoupper($change_after); } |
解码
protected function asciitostr($sacii){ $asc_arr= str_split(strtolower($sacii),2); $str=''; for($i=0;$i<count($asc_arr);$i++){ $str.=chr(hexdec($asc_arr[$i][1].$asc_arr[$i][0])); } return mb_convert_encoding($str,'UTF-8','GB2312'); } |
对排序的键进行转码
private function utf8_array_asort(&$array) { if (!isset($array) || !is_array($array)) { return false; } foreach ($array as $k => $v) { $array[$k] = self::strtoascii($v); } return true; } |
对数组数据进行转码 解码 ---英文不需要转码
public static function arrayMsort(&$data,$field) { $regions = array_column($data, $field); if(Yii::$app->language != "en"){ self::utf8_array_asort($regions); foreach($data as $key=>$val){ $data[$key]["name"] = self::strtoascii($val["name"]); } } array_multisort($regions, SORT_ASC, $data); foreach($data as $key=>$val){ if($val["code"] == 86){ unset($data[$key]); }else{ if(Yii::$app->language != "en"){ $data[$key]["name"] = self::asciitostr($val["name"]); } } } if(CConst::getIsOverseaClient() === false){ $cn = [['code'=>86,'name'=>Yii::$app->language=="en"?"China":"中国大陆"]]; $data = array_merge($cn,$data); } } |
转码前数据
[ { "code": 86, "name": "中国大陆" }, { "code": 1, "name": "美国" }, { "code": 81, "name": "日本" }, { "code": 49, "name": "德国" }, { "code": 44, "name": "英国" }, { "code": 33, "name": "法国" }, { "code": 91, "name": "印度" }, { "code": 39, "name": "意大利" }, { "code": 55, "name": "巴西" }, { "code": 1, "name": "加拿大" }, { "code": 82, "name": "韩国" }, { "code": 34, "name": "西班牙" }, { "code": 61, "name": "澳大利亚" }, { "code": 52, "name": "墨西哥" }, { "code": 73, "name": "俄罗斯" }, { "code": 62, "name": "印度尼西亚" }, { "code": 31, "name": "荷兰" }, { "code": 90, "name": "土耳其" }, { "code": 41, "name": "瑞士" }, { "code": 966, "name": "沙特阿拉伯" }, { "code": 54, "name": "阿根廷" }, { "code": 886, "name": "中国台湾" }, { "code": 48, "name": "波兰" }, { "code": 46, "name": "瑞典" }, { "code": 234, "name": "尼日利亚" }, { "code": 32, "name": "比利时" }, { "code": 98, "name": "伊朗" }, { "code": 47, "name": "挪威" }, { "code": 66, "name": "泰国" }, { "code": 43, "name": "奥地利" }, { "code": 60, "name": "马来西亚" }, { "code": 63, "name": "菲律宾" }, { "code": 27, "name": "南非" }, { "code": 852, "name": "中国香港" }, { "code": 972, "name": "以色列" }, { "code": 65, "name": "新加坡" }, { "code": 45, "name": "丹麦" }, { "code": 57, "name": "哥伦比亚" }, { "code": 353, "name": "爱尔兰共和国" }, { "code": 56, "name": "智利" }, { "code": 358, "name": "芬兰" }, { "code": 880, "name": "孟加拉国" }, { "code": 84, "name": "越南" }, { "code": 351, "name": "葡萄牙" }, { "code": 30, "name": "希腊" }, { "code": 420, "name": "捷克" }, { "code": 213, "name": "阿尔及利亚" }, { "code": 51, "name": "秘鲁" }, { "code": 964, "name": "伊拉克" }, { "code": 853, "name": "中国澳门" }, { "code": "1-649", "name": "土克凯可群岛" }, { "code": "1-787", "name": "波多黎各" }, { "code": "1-684", "name": "美属萨摩亚" }, { "code": "1-664", "name": "蒙塞拉特岛" }, { "code": "1-441", "name": "百慕大" }, { "code": "1-345", "name": "开曼群岛" }, { "code": 350, "name": "直布罗陀" }, { "code": 687, "name": "新喀里多尼亚" }, { "code": 594, "name": "法属圭亚那" }, { "code": 689, "name": "法属波利尼西亚" }, { "code": 596, "name": "马提尼克" }, { "code": 262, "name": "留尼汪" }, { "code": 508, "name": "圣皮耶与蜜克隆群岛" } ] |
转码后
[ { "code": 86, "name": "中国大陆" }, { "code": 213, "name": "阿尔及利亚" }, { "code": 54, "name": "阿根廷" }, { "code": 43, "name": "奥地利" }, { "code": 61, "name": "澳大利亚" }, { "code": "1-441", "name": "百慕大" }, { "code": 55, "name": "巴西" }, { "code": 353, "name": "爱尔兰共和国" }, { "code": 65, "name": "新加坡" }, { "code": 687, "name": "新喀里多尼亚" }, { "code": 32, "name": "比利时" }, { "code": 262, "name": "留尼汪" }, { "code": 48, "name": "波兰" }, { "code": "1-787", "name": "波多黎各" }, { "code": 60, "name": "马来西亚" }, { "code": 596, "name": "马提尼克" }, { "code": 98, "name": "伊朗" }, { "code": 964, "name": "伊拉克" }, { "code": 39, "name": "意大利" }, { "code": 972, "name": "以色列" }, { "code": 1, "name": "美国" }, { "code": "1-684", "name": "美属萨摩亚" }, { "code": 51, "name": "秘鲁" }, { "code": "1-664", "name": "蒙塞拉特岛" }, { "code": 880, "name": "孟加拉国" }, { "code": 91, "name": "印度" }, { "code": 62, "name": "印度尼西亚" }, { "code": 44, "name": "英国" }, { "code": 234, "name": "尼日利亚" }, { "code": 52, "name": "墨西哥" }, { "code": 27, "name": "南非" }, { "code": 84, "name": "越南" }, { "code": 49, "name": "德国" }, { "code": 45, "name": "丹麦" }, { "code": 47, "name": "挪威" }, { "code": 73, "name": "俄罗斯" }, { "code": 351, "name": "葡萄牙" }, { "code": 853, "name": "中国澳门" }, { "code": 886, "name": "中国台湾" }, { "code": 852, "name": "中国香港" }, { "code": 350, "name": "直布罗陀" }, { "code": 56, "name": "智利" }, { "code": 358, "name": "芬兰" }, { "code": 63, "name": "菲律宾" }, { "code": 33, "name": "法国" }, { "code": 689, "name": "法属波利尼西亚" }, { "code": 594, "name": "法属圭亚那" }, { "code": 57, "name": "哥伦比亚" }, { "code": 46, "name": "瑞典" }, { "code": 41, "name": "瑞士" }, { "code": 81, "name": "日本" }, { "code": 966, "name": "沙特阿拉伯" }, { "code": 31, "name": "荷兰" }, { "code": 82, "name": "韩国" }, { "code": 508, "name": "圣皮耶与蜜克隆群岛" }, { "code": 1, "name": "加拿大" }, { "code": 66, "name": "泰国" }, { "code": 420, "name": "捷克" }, { "code": 90, "name": "土耳其" }, { "code": "1-649", "name": "土克凯可群岛" }, { "code": 34, "name": "西班牙" }, { "code": "1-345", "name": "开曼群岛" }, { "code": 30, "name": "希腊" } ] |