如何按订单状态对数组进行排序

时间:2019-07-12 14:13:27

标签: php arrays

我有一个包含日期,状态和汇总的数组:

Array
(
    [0] => Array
        (
            [date] => 2019-07-06 14:01:52
            [status] => paid
            [summ] => 100

        )

    [1] => Array
        (
            [date] => 2019-07-02 12:22:31
            [summ] => 320
            [status] => pending
        )

    [2] => Array
        (
            [date] => 2019-07-01 18:43:58
            [summ] => 100
            [status] => pending
        )

    [3] => Array
        (
            [date] => 2019-06-26 14:01:52
            [status] => canceled
            [summ] => 200
        )

我需要先按状态(1.pending,2.paid,3.canceled)对数组进行排序,然后再按日期(顶部为new)进行排序。

我要的订单:

正在等待2019-07-02 12:22:31 等待中2019-07-01 18:43:58 支付2019-07-06 14:01:52 取消2019-06-26 14:01:52

到目前为止,我没有任何问题:

usort($new11, function($a, $b) {
    return strtotime($b['date']) - strtotime($a['date']);
});

但是如何按状态对我进行排序?

感谢:)

4 个答案:

答案 0 :(得分:3)

您只需使用array_multisort即可实现:

$test = [
    [
        'date' => '2019-07-06 14:01:52',
        'status' => 'paid',
        'summ' => 100,
    ],
    [
        'date' => '2019-07-02 12:22:31',
        'status' => 'pending',
        'summ' => 320,
    ],
    [
        'date' => '2019-07-01 18:43:58',
        'status' => 'pending',
        'summ' => 100,
    ],
    [
        'date' => '2019-06-26 14:01:52',
        'status' => 'canceled',
        'summ' => 200,
    ],
];

array_multisort(
    array_column($test, 'status'),
    SORT_DESC,
    array_column($test, 'date'),
    SORT_DESC,
    $test
);

var_dump($test);

结果将是:

array(4) {
  [0]=>
  array(3) {
    ["date"]=>
    string(19) "2019-07-02 12:22:31"
    ["status"]=>
    string(7) "pending"
    ["summ"]=>
    int(320)
  }
  [1]=>
  array(3) {
    ["date"]=>
    string(19) "2019-07-01 18:43:58"
    ["status"]=>
    string(7) "pending"
    ["summ"]=>
    int(100)
  }
  [2]=>
  array(3) {
    ["date"]=>
    string(19) "2019-07-06 14:01:52"
    ["status"]=>
    string(4) "paid"
    ["summ"]=>
    int(100)
  }
  [3]=>
  array(3) {
    ["date"]=>
    string(19) "2019-06-26 14:01:52"
    ["status"]=>
    string(8) "canceled"
    ["summ"]=>
    int(200)
  }
}

DEMO

答案 1 :(得分:0)

您可以使用asort对其进行排序,并按字母顺序对其进行排序。

如果您需要按某种状态顺序使用它,我建议使用int值来引用状态。可以使用一些约束来定义它们

答案 2 :(得分:0)

您快到了。您可以为优先级数组分配值。然后,您可以执行简单的数学运算来判断结果。

<?php

usort($new11, function($a, $b) {
    $priority = [
                    'pending' => 1,
                    'paid' => 2,
                    'canceled' => 3
                ];

    $result = $priority[$a['status']] - $priority[$b['status']];
    if($result === 0) return strtotime($b['date']) - strtotime($a['date']);
    return $result;    
});

答案 3 :(得分:0)

您可以使用Spaceship

<?php

    $new11 = [
        [
            'date' => '2019-07-06 14:01:52',
            'status' => 'paid',
            'summ' => 100,
        ],
        [
            'date' => '2019-07-02 12:22:31',
            'status' => 'pending',
            'summ' => 320,
        ],
        [
            'date' => '2019-07-01 18:43:58',
            'status' => 'pending',
            'summ' => 100,
        ],
        [
            'date' => '2019-06-26 14:01:52',
            'status' => 'canceled',
            'summ' => 200,
        ],
    ];

    $pending = 3;
    $paid = 2;
    $canceled = 1;

    usort($new11, function($a, $b) use ($pending, $paid, $canceled) {
        return (${$b['status']} <=> ${$a['status']}) * 10 +
               (strtotime($b['date']) <=> strtotime($a['date']));
    });


    var_dump($new11);

demo