php数组中文排序,文件格式一般用utf8,直接用asort排序不行。若是gbk和gb2312可以。这跟编码有关。gbk和gb2312本身的编码就是用拼音排序的。
复制代码 代码如下:
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;
}
使用例子:
复制代码 代码如下:
$abc = array('a'=>'猜', 'b'=>'我','c'=>'哦','d'=>'棍','e'=>'f','f'=>'爸','z'=>'州');
utf8_array_asort($abc);
print_r($abc);
但是使用这个函数发现,有些文字会出错,可能是utf8编码认不出某些字导致的“非法字符”,据了解GBK字符集比较大,换成GBK,再加上IGNORE忽略掉不认识的字符 ,改成下面这样
复制代码 代码如下:
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;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
