0

我有这个示例数组,我想按以下顺序对每个第一级项目(66000, 66001, 66002)进行排序:New Store列表中的第一个,列表中的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
        )
    )
)
4

2 回答 2

0

在 usort 中您需要做的就是定义如何订购任何 2 个给定的项目。

在你的情况下,如果你的数组数据被整理了一下,通过从月份数据中取出状态会更容易。照原样,我们必须假设所有月份都是一样的。

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

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;
   }
});

如果密钥对您很重要,您需要考虑使用 uasort,因为 usort 不会保留密钥。

于 2013-08-19T18:59:04.083 回答
-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];
}

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

于 2013-08-19T19:01:31.577 回答