开发者

Fastest way of sorting this multidimensional array?

What is the fastest way of sorting this both alphabetically by country and numerically b开发者_高级运维y date?:

Array
(
[JAPAN] => Array
    (
        [2010-10-17] => 2
    )

[CUBA] => Array
    (
        [2010-10-16] => 9
    )

[RUSSIAN FEDERATION] => Array
    (
        [2010-10-16] => 26
        [2010-10-17] => 6
        [2010-10-18] => 2
    )

[CHINA] => Array
    (
        [2010-10-16] => 13
    )

)


foreach ($array as $value) {

    ksort($value);
}

ksort($array);

http://codepad.org/wJn0hJN4

array(4) {
  ["CHINA"]=>
  array(1) {
    ["2010-10-16"]=>
    int(13)
  }
  ["CUBA"]=>
  array(1) {
    ["2010-10-16"]=>
    int(9)
  }
  ["JAPAN"]=>
  array(1) {
    ["2010-10-17"]=>
    int(2)
  }
  ["RUSSIAN FEDERATION"]=>
  array(3) {
    ["2010-10-16"]=>
    int(26)
    ["2010-10-17"]=>
    int(6)
    ["2010-10-18"]=>
    int(2)
  }
}


You will have to benchmark on more data. I'd try ksort for sorting by countries and usort for sorting by dates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜