This article mainly introduces the sorting method of php arrays containing Chinese characters. if you need a php array, you can refer to the Chinese sorting method of php arrays. Generally, the file format is utf8, but it cannot be sorted directly by asort. It can be gbk or gb2312. This is related to encoding. Gbk and gb2312 are encoded in pinyin.
The code is as follows:
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;
}
Example:
The code is as follows:
$ Abc = array ('a' => 'ta', 'B' => 'I', 'C' => 'Oh ', 'D' => 'batch', 'E' => 'F', 'F' => 'Dad', 'Z' => 'status ');
Utf8_array_asort ($ abc );
Print_r ($ abc );
However, when using this function, it is found that some characters may be wrong. it may be because utf8 encoding does not recognize some characters, resulting in "invalid characters". It is understood that the GBK character set is relatively large and changed to GBK, add IGNORE to IGNORE unknown characters and change it to the following:
The code is as follows:
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;
}