I currently have a multidimensional array I am sorting using array_multisort, however one of the columns has some special accented characters - for example Développé
.
Is there a way to sort these characters with their equivalent non accented letter é
= e
?
array_multisort($column1, SORT_ASC,SORT_STRING|SORT_FLAG_CASE, $column2, SORT_ASC,SORT_STRING|SORT_FLAG_CASE, $multid_array);
There are a few answers here on SO regarding this issue, but when I tried them in the past, it seemed that most of them were too dependant on the locale of the system or even str_replace with incomplete arrays . So running the code would either create different results on different environment or would miss a couple of characters.
In the end I found the best approach comes from this non-accepted answer . From my tests this approach works perfectly, independently from all local settings, and can be adapted for accents only è
or different sets like æ
The base is the Transliterator class , part of the intl extension. which is available from PHP 5.4 onwards. In most system it's either already bundled or can be installed with
pecl install intl`
More installation info here .
How to use is quite straightforward
$transliterator = Transliterator::createFromRules(':: NFD; :: [:Nonspacing Mark:] Remove; :: NFC;', Transliterator::FORWARD);
For a list of rules that can be applied to this function, please refer to this manual . The ones used in the example already work for your specific use case.
$newstring = $transliterator->transliterate($string);
In your specific case, you need of course to apply to all the relevant elements of your array.
You're not sharing the array structure, so I can not produce a clear example, but depending on the structure you can use foreach
or array_walk or recursive functions to loop and change where needed. If you want to update your initial question with a sample from the array, we can be more precise.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.