-
- function utf8_array_asort(&$array) {
- if(!isset($array) || !is_array($array)) {
- return false;
- }
- foreach($array as $ k=>$v) {
- $array[$k] = iconv('UTF-8', 'GB2312',$v);
- }
- asort($array);
- foreach($array as $k= >$v) {
- $array[$k] = iconv('GB2312', 'UTF-8', $v);
- }
- return true;
- }
Copy code
Call example:
-
- $abc = array('a'=>'guess', 'b'=>'me','c'=>'oh','d'=>'stick' ,'e'=>'f','f'=>'Dad','z'=>'State');
- utf8_array_asort($abc);
- print_r($abc);
Copy Code
But using this function, I found that some text will be wrong. It may be "illegal characters" caused by the utf8 encoding not recognizing some characters. It is understood that the GBK character set is relatively large, so I changed it to GBK, and IGNORE ignored it. For characters you don’t recognize, change them to the following:
-
- private function utf8_array_asort(&$array) {
- if(!isset($array) || !is_array($array)) {
- return false;
- }
- foreach($array as $k=>$v) {
- $array[$k] = iconv('UTF-8', 'GBK//IGNORE',$v);
- }
- asort($array);
- foreach($ array as $k=>$v) {
- $array[$k] = iconv('GBK', 'UTF-8//IGNORE', $v);
- }
- return true;
- }
Copy Code
|