PHP Chinese Array Sorting Method Example

Source: Internet
Author: User
    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

Invocation Example:

    1. $ABC = Array (' A ' = ' guess ', ' b ' = = ' I ', ' c ' = ' Oh ', ' d ' = ' stick ', ' e ' + ' f ', ' f ' = = ' Dad ', ' z ' = ' state ');
    2. Utf8_array_asort ($ABC);
    3. Print_r ($ABC);
Copy Code

However, using this function to find that some of the text will be wrong, it may be that the UTF8 code does not recognize some words caused by "illegal characters", it is understood that the GBK character set is larger, replaced by GBK, plus ignore ignore the characters that do not know, change 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
  • 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.