Php Chinese array sorting method example
- 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;
- }
Call example:
- $ 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:
- 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;
- }
|