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;
}

原因是什么?

作为回复发布,因为这作为评论会非常难看:

我不能复制你的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"
}

正如您所看到的,Ö确实排序不正确,但它排序到数组的END,而不是开头。

我想我的答案太晚了,但数组似乎已经按照Country Object的stringIndex字段进行了排序。

也许(只是也许,我不知道确切的原因)因为SORT_STRING标志,ksort尝试填充具有定义的string类型的元素,但如果不能,那么它会尝试在Country对象中查找具有"字符串"关键字的键,如stringIndex

相关内容

  • 没有找到相关文章

最新更新