对这个多维数组进行排序的最快方法?

发布于 2024-09-28 06:38:19 字数 392 浏览 1 评论 0原文

按国家/地区字母顺序和日期数字顺序排序的最快方法是什么?:

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
    )

)

What is the fastest way of sorting this both alphabetically by country and numerically by 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
    )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-10-05 06:38:19
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)
  }
}
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)
  }
}
温暖的光 2024-10-05 06:38:19

您将必须对更多数据进行基准测试。我会尝试 ksort 按国家/地区和 usort 用于按日期排序。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文