質問

私はスペイン語の言語の名前を保持する配列を持っています:

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

アレイを注文してインデックスの関連付けを維持する必要があるので、 asort()を使用する必要がある。strong> 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)

    しかし、これが私が受けているものです:

    • 配列([KO]=> COREANO [ES]=>ESPAÑOL[FR]=>FRANCÉS[AR]=>ÁRABE)

      何かが足りない?ご意見ありがとうございます!(私のサーバーはPHPバージョン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);

他のヒント

You defined your locale incorrectly in setlocale().

Change:

setlocale(LC_ALL,'es_ES.UTF-8');

To:

setlocale(LC_ALL,'es_ES');

Output:

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

The documentation for setlocale mentions that

Different systems have different naming schemes for locales.

It's possible that your system does not recognize the locale as es_ES. If you are on Windows, try esp_ESP instead.

Try this

setlocale(LC_COLLATE, 'nl_BE.utf8');
$array = array('coreano','árabe','español','francés');
usort($array, 'strcoll'); 
print_r($array);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top