Исходный массив состоит в следующем:ksort() производит, как неправильный результат
Array
(
[Danmark] => Country Object
(
[id:protected] => 39
[name:protected] => Danmark
[code:protected] => DK
[stringIndex:protected] => DENMARK
)
[Tyskland] => Country Object
(
[id:protected] => 59
[name:protected] => Tyskland
[code:protected] => DE
[stringIndex:protected] => GERMANY
)
[Irland] => Country Object
(
[id:protected] => 78
[name:protected] => Irland
[code:protected] => IE
[stringIndex:protected] => IRELAND
)
[Italien] => Country Object
(
[id:protected] => 81
[name:protected] => Italien
[code:protected] => IT
[stringIndex:protected] => ITALY
)
[Holland] => Country Object
(
[id:protected] => 119
[name:protected] => Holland
[code:protected] => NL
[stringIndex:protected] => NETHERLANDS
)
[Nya Zeeland] => Country Object
(
[id:protected] => 122
[name:protected] => Nya Zeeland
[code:protected] => NZ
[stringIndex:protected] => NEW_ZEALAND
)
[Polen] => Country Object
(
[id:protected] => 138
[name:protected] => Polen
[code:protected] => PL
[stringIndex:protected] => POLAND
)
[Spanien] => Country Object
(
[id:protected] => 161
[name:protected] => Spanien
[code:protected] => ES
[stringIndex:protected] => SPAIN
)
[Sverige] => Country Object
(
[id:protected] => 166
[name:protected] => Sverige
[code:protected] => SE
[stringIndex:protected] => SWEDEN
)
[Schweiz] => Country Object
(
[id:protected] => 167
[name:protected] => Schweiz
[code:protected] => CH
[stringIndex:protected] => SWITZERLAND
)
[England] => Country Object
(
[id:protected] => 185
[name:protected] => England
[code:protected] => GB
[stringIndex:protected] => UNITED_KINGDOM
)
[Osterrike] => Country Object
(
[id:protected] => 197
[name:protected] => Osterrike
[code:protected] => AT
[stringIndex:protected] => AUSTRIA
)
[Belgien] => Country Object
(
[id:protected] => 236
[name:protected] => Belgien
[code:protected] => BE
[stringIndex:protected] => BELGIUM
)
)
И после того, как я называю:
ksort($countries, SORT_STRING);
я получаю это:
Array
(
[Osterrike] => Country Object
(
[id:protected] => 197
[name:protected] => Osterrike
[code:protected] => AT
[stringIndex:protected] => AUSTRIA
)
[Belgien] => Country Object
(
[id:protected] => 236
[name:protected] => Belgien
[code:protected] => BE
[stringIndex:protected] => BELGIUM
)
[Danmark] => Country Object
(
[id:protected] => 39
[name:protected] => Danmark
[code:protected] => DK
[stringIndex:protected] => DENMARK
)
[Tyskland] => Country Object
(
[id:protected] => 59
[name:protected] => Tyskland
[code:protected] => DE
[stringIndex:protected] => GERMANY
)
[Irland] => Country Object
(
[id:protected] => 78
[name:protected] => Irland
[code:protected] => IE
[stringIndex:protected] => IRELAND
)
[Italien] => Country Object
(
[id:protected] => 81
[name:protected] => Italien
[code:protected] => IT
[stringIndex:protected] => ITALY
)
[Holland] => Country Object
(
[id:protected] => 119
[name:protected] => Holland
[code:protected] => NL
[stringIndex:protected] => NETHERLANDS
)
[Nya Zeeland] => Country Object
(
[id:protected] => 122
[name:protected] => Nya Zeeland
[code:protected] => NZ
[stringIndex:protected] => NEW_ZEALAND
)
[Polen] => Country Object
(
[id:protected] => 138
[name:protected] => Polen
[code:protected] => PL
[stringIndex:protected] => POLAND
)
[Spanien] => Country Object
(
[id:protected] => 161
[name:protected] => Spanien
[code:protected] => ES
[stringIndex:protected] => SPAIN
)
[Sverige] => Country Object
(
[id:protected] => 166
[name:protected] => Sverige
[code:protected] => SE
[stringIndex:protected] => SWEDEN
)
[Schweiz] => Country Object
(
[id:protected] => 167
[name:protected] => Schweiz
[code:protected] => CH
[stringIndex:protected] => SWITZERLAND
)
[England] => Country Object
(
[id:protected] => 185
[name:protected] => England
[code:protected] => GB
[stringIndex:protected] => UNITED_KINGDOM
)
)
Когда я проверяю он с теми же индексами, но со значениями простых строк вместо моих объектов Country
, он сортируется правильно. Когда я тестирую его с теми же индексами, но с пустыми объектами Test
вместо объектов Country
, он снова сортируется правильно. Но в этом конкретном случае он возвращает неверный результат. Они не сортируются никакими значениями внутри объекта, все значения кажутся случайными.
Country
класс предельно прост:
class Country {
protected $id;
protected $name;
protected $code;
protected $stringIndex;
}
Что может по причине?
Действительно ли это «Osterrike», или что-то вроде «Österrike»? Я не верю, что функции сортировки массива не знают обо всех юникодах. –
Это 'Osterrike', потому что я удалил' Ö' для тестирования. Этот персонаж был моим первым подозрением. –
Затем попробуйте вручную заменить версию umlauted равным ascii O и посмотреть, правильно ли она сортируется. Нет никакого способа, которым ksort потерпит неудачу по тексту ascii, но я поспорю, что вы блестящий пенни, он испортит версии с акцентом/юникодом. –