ksort()产生不正确的结果

时间:2014-11-10 14:32:54

标签: php sorting 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;

}

原因是什么?

2 个答案:

答案 0 :(得分:0)

作为一个回答发布,因为这将是一个非常丑陋的评论:

我无法复制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,而不是开头。

答案 1 :(得分:-1)

我认为,我的答案为时已晚,但似乎数组已按国家/地区对象的stringIndex字段排序。

也许(只是可能,我不确切地知道)因为SORT_STRING标志ksort尝试使用定义的string类型的元素,但是如果它不能#t; t然后,它尝试用&#34; string&#34;找到键。国家/地区对象中的stringIndex关键字。