按定义的顺序对多维数组进行排序

时间:2013-08-19 18:19:31

标签: php usort

我有这个示例数组,我想按顺序对每个第一级项目660006600166002)进行排序:New Store首先在list,Store Relocation列表中的第二个,并且我没有在示例数组中包含Underperforming Stores,但这些将是最后一个。然后需要按open日期排序那些如何以这种方式对它们进行排序?

我相信我需要使用usort来完成此任务,但我并不真正理解usort函数,我可以获得这方面的帮助吗?

到目前为止,这是我的功能......

usort($mainpgArr, function($a, $b){

});

示例原始数组:

Array(
    [66000] => Array(
        [January] => Array(
            [status] => New Store
            [sales] => 100.00
            [open] => 2013-05-01
        )
        [February] => Array(
            [status] => New Store
            [sales] => 200.00
            [open] => 2013-05-01
        )
        [March] => Array(
            [status] => New Store
            [sales] => 140.00
            [open] => 2013-05-01
        )
    )
    [66001] => Array(
        [January] => Array(
            [status] => Store Relocation
            [sales] => 3400.00
            [open] => 2013-07-01
        )
        [February] => Array(
            [status] => Store Relocation
            [sales] => 1340.00
            [open] => 2013-07-01
        )
        [March] => Array(
            [status] => Store Relocation
            [sales] => 1550.00
            [open] => 2013-07-01
        )
    )
    [66002] => Array(
        [January] => Array(
            [status] => New Store
            [sales] => 1050.00
            [open] => 2013-01-01
        )
        [February] => Array(
            [status] => New Store
            [sales] => 1009.00
            [open] => 2013-01-01
        )
        [March] => Array(
            [status] => New Store
            [sales] => 1020.00
            [open] => 2013-01-01
        )
    )
)

示例最终数组

Array(
    [66002] => Array(
        [January] => Array(
            [status] => New Store
            [sales] => 1050.00
            [open] => 2013-01-01
        )
        [February] => Array(
            [status] => New Store
            [sales] => 1009.00
            [open] => 2013-01-01
        )
        [March] => Array(
            [status] => New Store
            [sales] => 1020.00
            [open] => 2013-01-01
        )
    )
    [66000] => Array(
        [January] => Array(
            [status] => New Store
            [sales] => 100.00
            [open] => 2013-05-01
        )
        [February] => Array(
            [status] => New Store
            [sales] => 200.00
            [open] => 2013-05-01
        )
        [March] => Array(
            [status] => New Store
            [sales] => 140.00
            [open] => 2013-05-01
        )
    )
    [66001] => Array(
        [January] => Array(
            [status] => Store Relocation
            [sales] => 3400.00
            [open] => 2013-07-01
        )
        [February] => Array(
            [status] => Store Relocation
            [sales] => 1340.00
            [open] => 2013-07-01
        )
        [March] => Array(
            [status] => Store Relocation
            [sales] => 1550.00
            [open] => 2013-07-01
        )
    )
)

2 个答案:

答案 0 :(得分:0)

你需要做的就是定义如何订购任何两个给定的物品。

在您的情况下,通过从月份数据中获取状态,您的阵列数据会稍微整理一下会更容易。按原样,我们必须假设它在所有月份都是相同的。

根据这个假设,我们将使用前几个月的状态进行比较。

usort($mainpgArr, function($a, $b){
   if ($a[0]['status'] == $b[0]['status']) {
       // status equal so sort by open date
       if ($a[0]['open'] == $b[0]['open']) {
           return 0;
       } else {
           return ($a[0]['open'] > $b[0]['open']) ? 1 : -1;
       } 
   } else {
       // sorting by status
       // we can use a simple comparison operation as your desired order happens to be alphabetical
       return ($a[0]['status'] > $b[0]['status']) ? 1 : -1;
   }
});

如果密钥对您很重要,则需要look at using uasort,因为usort不会保留密钥。

答案 1 :(得分:-1)

你做得不对。

这很简单: http://www.php.net/manual/en/function.ksort.php

ksort($mainpgArr);

这就是所有人。现在$ mainpgArr按你想要的方式排序。要对多维部分进行排序,请使用asort();


抱歉,误解了这个问题。好的,你需要用ksort()对键进行排序。然后循环遍历整个事情,创建一个带有排序键的新数组,同时asort()索引..

示例:

ksort($mainpgArr);
foreach($mainpgArr as $key => $value) {
  asort($mainpgArr[$key];
}

应该这样做。抱歉,我之前没有更具体。 (我没有测试过这个,但是应该工作)