在PHP中对带有特殊字符的数组进行排序 [英] Sort an array with special characters in PHP

查看:217
本文介绍了在PHP中对带有特殊字符的数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其中保存着西班牙语的语言名称:

I have an array that holds the names of languages in spanish:

$lang["ko"] = "coreano"; //korean
$lang["ar"] = "árabe"; //arabic
$lang["es"] = "español"; //spanish
$lang["fr"] = "francés"; //french

我需要对数组进行排序并维护索引关联,因此我将 asort() SORT_LOCALE_STRING

I need to order the array and maintain index association, so I use asort() with the SORT_LOCALE_STRING

setlocale(LC_ALL,'es_ES.UTF-8'); //this is at the beginning (config file)
asort($lang,SORT_LOCALE_STRING);
print_r($lang);

预期输出将按以下顺序:

The expected output would be in this order:

  • 数组([ar] =>árabe[ko] => coreano [es] =>西班牙语[fr] =>francés)

但是,这是我收到的:

  • Array([ko] => coreano [es] =>español[fr] =>francés[ar] =>árabe)

我错过了什么吗?感谢您的反馈意见! (我的服务器使用的是PHP 5.2.13版)

Am I missing something? Thanks for your feedback! (my server is using PHP Version 5.2.13)

推荐答案

尝试按翻译的名称进行排序:

Try sorting by translitterated names:

function compareASCII($a, $b) {
    $at = iconv('UTF-8', 'ASCII//TRANSLIT', $a);
    $bt = iconv('UTF-8', 'ASCII//TRANSLIT', $b);
    return strcmp($at, $bt);
}

uasort($lang, 'compareASCII');

print_r($lang);

这篇关于在PHP中对带有特殊字符的数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆