Is it possible to sort an array with Unicode / UTF-8 characters in PHP using a natural order algorithm? For example (the order in this array is correctly ordered):
$array = array
(
0 => 'Agile',
1 => 'ágile',
2 => 'àgile',
3 => '?gile',
4 => '?gile',
5 => '?gile',
6 => 'Test',
);
If I try with asort($array) I get the following result:
Array
(
[0] => Agile
[6] => Test
[2] => àgile
[1] => ágile
[3] => ?gile
[5] => ?gile
[4] => ?gile
)
And using natsort($array):
Array
(
[2] => àgile
[1] => ágile
[3] => ?gile
[5] => ?gile
[4] => ?gile
[0] => Agile
[6] => Test
)
How can I implement a function that returns the correct result order (0, 1, 2, 3, 4, 5, 6) under PHP 5? All the multi byte string functions (mbstring, iconv, ...) are available on my system.
EDIT: I want to natsort() the values, not the keys - the only reason why I'm explicitly defining the keys (and using asort() instead of sort()) is to ease the job of finding out where the sorting of unicode values went wrong.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…