PHP Chinese array sorting method example

WBOY
Release: 2016-07-25 08:53:19
Original
890 people have browsed it
  1. function utf8_array_asort(&$array) {
  2. if(!isset($array) || !is_array($array)) {
  3. return false;
  4. }
  5. foreach($array as $ k=>$v) {
  6. $array[$k] = iconv('UTF-8', 'GB2312',$v);
  7. }
  8. asort($array);
  9. foreach($array as $k= >$v) {
  10. $array[$k] = iconv('GB2312', 'UTF-8', $v);
  11. }
  12. return true;
  13. }
Copy code

Call example:

  1. $abc = array('a'=>'guess', 'b'=>'me','c'=>'oh','d'=>'stick' ,'e'=>'f','f'=>'Dad','z'=>'State');
  2. utf8_array_asort($abc);
  3. 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:

  1. private function utf8_array_asort(&$array) {
  2. if(!isset($array) || !is_array($array)) {
  3. return false;
  4. }
  5. foreach($array as $k=>$v) {
  6. $array[$k] = iconv('UTF-8', 'GBK//IGNORE',$v);
  7. }
  8. asort($array);
  9. foreach($ array as $k=>$v) {
  10. $array[$k] = iconv('GBK', 'UTF-8//IGNORE', $v);
  11. }
  12. return true;
  13. }
Copy Code


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