Sorting PHP array with locale setting?

帅比萌擦擦* 提交于 2021-02-16 20:17:55

问题


Is it possible to sort a PHP array with a locale setting?

This is the setup:

I am making an interactive sorted list in PHP. By user input, one of a number of categories (columns) can be made to direct the sorting (name, residence, etc). This I worked out by using array_multisort() function.

Next hurdle. The list is in Swedish and the user will expect Swedish alphabetical order: abcdefghijklmnopqrstuvxyzåäö. Right now the interpreter sorts åäö as non-alphabetical and places them before "a". How to remedy?

I found some scattered info on a setlocale(LC_COLLATE, "sv_SV") function, but the reviews were not rave and I did not manage to comprehend how it could be used with array_multisort(). Can it? and if so, how? Is there another way within php?

Thing is, there must be some way – Swedish websites abound where Swedish sort order is applied. Can it be done with php?


回答1:


Use SORT_LOCALE_STRING as the third parameter of array_multisort() function. PHP ducuments say:

SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()

example:

$result=array_multisort( $input_array, SORT_ASC, SORT_LOCALE_STRING);



回答2:


Use collator_sort or collator_asort.

// sorting properly accented "Č" in Czech language (should come after "C" and before "D")

// 1) Simple sort:
$array = ['bca', 'čaz', 'cba', 'abc', 'daz'];
$collator = collator_create('cs-CZ');
collator_sort($collator, $array);
// result:
// ['abc', 'bca', 'cba', 'čaz', 'daz']

// 2) Maintain index assotiations:
$array = [
    'x' => 'Česko',
    'y' => 'Dänmark',
    'z' => 'Brunei',
    'w' => 'Cyprus'
];
$coll = collator_create('sk'); // set Slovak locale (or sk-SK)
collator_asort($coll, $array);

/* result:
$array = [
    'z' => 'Brunei',
    'w' => 'Cyprus',
    'x' => 'Česko',
    'y' => 'Dänmark',
];
*/



回答3:


Sorry. This was not the problem I thought it was. Text written in my code gets sorted sort of correctly (only that the ä gets incorrectly sorted before å, but that seems to a bug in the specs(?)).

Anyway, the problem is obviously with character encoding. It is when text is fetched from a Contact Form 7 database (Wordpress plugin) that the problem arises. Presumably it has another encoding and needs converting.

Thanks anyway.



来源:https://stackoverflow.com/questions/43212197/sorting-php-array-with-locale-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!