首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ksort()生成的结果不正确

ksort()生成的结果不正确
EN

Stack Overflow用户
提问于 2014-11-10 22:32:54
回答 2查看 337关注 0票数 1

原始数组如下所示:

代码语言:javascript
复制
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
        )

)

在我打电话给你之后:

代码语言:javascript
复制
ksort($countries, SORT_STRING);

我明白了:

代码语言:javascript
复制
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类非常简单:

代码语言:javascript
复制
class Country {

   protected $id;
   protected $name;
   protected $code;
   protected $stringIndex;

}

原因是什么呢?

EN

回答 2

Stack Overflow用户

发布于 2014-11-10 23:00:18

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

我不能先复制你的O

代码:

代码语言:javascript
复制
<?php

$foo = array(
   'P' => 'regular p',
   'Ö' => 'umlaut o',
   'O' => 'regular o',
   'A' => 'regular A'
);

var_dump($foo);ksort($foo);var_dump($foo);

结果:

代码语言:javascript
复制
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"
}

正如您所看到的,Ö排序不正确,但它排序到数组的末尾,而不是开始。

票数 0
EN

Stack Overflow用户

发布于 2017-10-31 12:56:29

我的回答太晚了,我想,但数组似乎已经按Country对象的stringIndex字段排序了。

可能(可能是,可能是,我不太清楚)因为SORT_STRING标志ksort尝试查找具有已定义字符串类型的元素,但如果不能,则会尝试在Country对象中查找带有“string”关键字的键,如stringIndex

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26846115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档