Home > Backend Development > PHP Tutorial > Questions about Chinese sorting

Questions about Chinese sorting

WBOY
Release: 2016-08-04 09:21:55
Original
1105 people have browsed it

My data source is in utf8 format, but I need to sort the data. Since it is Chinese, I converted it to gbk or gbk2312, but some inaccuracies occurred in the sorting

I sorted using the array_multi_sort method of php

Such a problem will occur in the sorting results

Questions about Chinese sorting

The data in the black box is obviously incorrect in descending order of pinyin. However, the subsequent data are all appropriate. I don’t know the reason for this? Or is it that the gbk library is not complete enough?

Add a test code

<code>$array = array("鹭","昊","瀚","骅","众","怡","萃","重","恺","鑫");
foreach($array as $val){
    $newArr[] = iconv('utf8','GBK',$val);
}

array_multisort($newArr,SORT_DESC,SORT_FLAG_CASE );
echo "<pre class="brush:php;toolbar:false">";
print_r($newArr);
die;</code>
Copy after login
Copy after login

Reply content:

My data source is in utf8 format, but I need to sort the data. Since it is Chinese, I converted it to gbk or gbk2312, but some inaccuracies occurred in the sorting

I sorted using the array_multi_sort method of php

Such a problem will occur in the sorting results

Questions about Chinese sorting

The data in the black box is obviously incorrect in descending order of pinyin. However, the subsequent data are all appropriate. I don’t know the reason for this? Or is it that the gbk library is not complete enough?

Add a test code

<code>$array = array("鹭","昊","瀚","骅","众","怡","萃","重","恺","鑫");
foreach($array as $val){
    $newArr[] = iconv('utf8','GBK',$val);
}

array_multisort($newArr,SORT_DESC,SORT_FLAG_CASE );
echo "<pre class="brush:php;toolbar:false">";
print_r($newArr);
die;</code>
Copy after login
Copy after login

If the sorting result is not necessarily accurate according to the pinyin of Chinese characters, you can also sort according to the ascii of Chinese characters, but the program complexity will increase. If your classification data is read from the database, you can try adding a sort field in the database

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template