English array sorting in PHP

Source: Internet
Author: User
Tags intl
PHP array contains both Chinese and English, how to sort the first word or Chinese characters

Reply content:

PHP array contains both Chinese and English, how to sort the first word or Chinese characters

PHP has a lot of functions for sorting arrays.

For example, the most basic sort and rsort (plus r The representative reverse order, the same below);
The natural sort natsort (natural sort is to sort the numbers by size, not by ASCII code) and ignore the case natcasesort ;
Keep the key-value index relationship asort arsort ;
The and of the sort according to the key ksort krsort ;
If you are not satisfied with the built-in collation, you can also write your own function to determine the collation usort , uasort and uksort (the corresponding function does not have u the above);
There is also the ability to sort multiple arrays and keep only the key-value index relationships of the associated array array_multisort .

There is always one for you in so many sorts.
Please refer to the documentation for details.

 !--? Phpheader (' Content-type:text/plain; Charset=utf-8 '); $arr = Explode ("', ' 1 111" 121 122 a AA AAA AaB AB ABA ABB Ali Baidu China Chinese China China Middle China mid-Chinese medium '); Shuffle ($arr); Scrambled Array//collator_sort (collator_create (' zh_cn '), $arr),//usort ($arr, function ($a, $b) {return strnatcmp ($a, $b);}); Usort ($arr, function ($a, $b) {return strcmp ($a, $b);}); Echo Implode (", $arr); exit ();? -->ls sorted by: 1 111 121 122 a AA AAA AaB AB ABA ABB Ali Baidu China China China Chinese Middle China mid-central Chinese middle-medium-central collator_sort (ZH_CN) sort (similar to Windows/linu X Desktop File Manager default by name Ascending): 1 111 121 122 a AA AAA AaB AB ABA ABB Ali Baidu China Chinese China China Middle Central China Central strnatcmp Sort: 1 11 12 111 121 122 a AA AAA AaB AB ABA ABB Zhong Zhong Zhong Chinese china China China Chinese state Baidu Arrey strcmp Sort: 1 111, 121 122 a AA AAA AAB AB ABA ABB Chinese, Chinese, China, China Chinese, China, Baidu Arrey  

Using the strcmp string to compare the ordering has actually been able to meet your needs.
STRNATCMP Natural Sort and strcmp string comparison sort the main difference is that the numbers, strnatcmp processed by the number elements are from small to large sort, there is a size relationship.
In the ICU Extension Intl, the Simplified Chinese sequencer collator_sort (ZH_CN) sorting is mainly based on the pinyin of the Chinese characters, such as Ali,baidu,zhongguo. Interested can also see my article PHP uses ICU Extension Intl To quickly implement Chinese characters to pinyin and to sort alphabetically by the first letter of Pinyin, Chinese characters to Pinyin is a function of things:

echo transliterator_transliterate('Any-Latin; Latin-ASCII; Upper()', '中华有为');//输出 ZHONG HUA YOU WEI
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.