dongzong8110 2013-08-19 18:19
浏览 27

按定义的顺序对多维数组进行排序[重复]

This question already has an answer here:

I have this example array, and I would like to sort each first level item (66000, 66001, 66002) in this order: New Store first in the list, Store Relocation second in the list, and I didn't include Underperforming Stores in the example array, but those would be last. those then need to be sorted by open date How can I sort them this way?

I believe I need to use usort to accomplish this, but I don't really understand the usort function, can I get help with this?

Here is my function so far...

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

});

Example Original Array:

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

Example Final Array

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
        )
    )
)
</div>
  • 写回答

2条回答 默认 最新

  • douju2331 2013-08-19 18:59
    关注

    All you need to do in usort is define how any 2 given items should be ordered.

    In your case it would be easier if your array data was tidied up a bit, by taking the status out of the month data. As is, we'll have to assume it will be the same for all months.

    With that assumption made we will use the first months' status for our comparison.

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

    If the keys are important to you you'll need to look at using uasort as usort doesn't preserve keys.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么