原始数组如下所示:
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;
}原因是什么呢?
发布于 2014-11-10 23:00:18
发布为回复,因为这将是一个非常丑陋的评论:
我不能先复制你的O:
代码:
<?php
$foo = array(
'P' => 'regular p',
'Ö' => 'umlaut o',
'O' => 'regular o',
'A' => 'regular A'
);var_dump($foo);ksort($foo);var_dump($foo);
结果:
array(4) {
["P"]=>
string(9) "regular p"
["Ö"]=>
string(8) "umlaut o"
["O"]=>
string(9) "regular o"
["A"]=>
string(9) "regular A"
}
array(4) {
["A"]=>
string(9) "regular A"
["O"]=>
string(9) "regular o"
["P"]=>
string(9) "regular p"
["Ö"]=>
string(8) "umlaut o"
}正如您所看到的,Ö排序不正确,但它排序到数组的末尾,而不是开始。
发布于 2017-10-31 12:56:29
我的回答太晚了,我想,但数组似乎已经按Country对象的stringIndex字段排序了。
可能(可能是,可能是,我不太清楚)因为SORT_STRING标志ksort尝试查找具有已定义字符串类型的元素,但如果不能,则会尝试在Country对象中查找带有“string”关键字的键,如stringIndex。
https://stackoverflow.com/questions/26846115
复制相似问题