gpt4 book ai didi

php - 在 PHP 中对具有特殊字符的数组进行排序

转载 作者:可可西里 更新时间:2023-10-31 22:12:57 24 4
gpt4 key购买 nike

我有一个数组,其中包含西类牙语的语言名称:

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

我需要对数组进行排序并维护索引关联,所以我使用 asort()使用 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);

预期的输出将按以下顺序:

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

但是,这是我收到的:

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

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

最佳答案

尝试按音译名称排序:

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 - 在 PHP 中对具有特殊字符的数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10649473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com