PHP-Usort多维数组

我正在尝试使用以下usort函数按voucherdate和vouchernumber的降序对数组进行排序

usort($voucherdata, function($a, $b) {
   return [$b['voucherdate'], $b['vouhcernumber']] <=> [$a['voucherdate'], $a['vouhcernumber']];
});

但是,某些值的排列顺序不正确。如下面的输出所示:

0: {vouchernumber: "51", vouchertype: "JV", voucherdate: "2020-05-15", enteries: Array(2)}
1: {vouchernumber: "39", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}
2: {vouchernumber: "50", vouchertype: "JV", voucherdate: "2020-05-14", enteries: Array(2)}
3: {vouchernumber: "49", vouchertype: "JV", voucherdate: "2020-05-14", enteries: Array(2)}
4: {vouchernumber: "48", vouchertype: "JV", voucherdate: "2020-05-14", enteries: Array(2)}
5: {vouchernumber: "33", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(4)}
6: {vouchernumber: "38", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}
7: {vouchernumber: "34", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}
8: {vouchernumber: "37", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}
9: {vouchernumber: "35", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}

我希望的输出是:

0: {vouchernumber: "51", vouchertype: "JV", voucherdate: "2020-05-15", enteries: Array(2)}
1: {vouchernumber: "39", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}
2: {vouchernumber: "50", vouchertype: "JV", voucherdate: "2020-05-14", enteries: Array(2)}
3: {vouchernumber: "49", vouchertype: "JV", voucherdate: "2020-05-14", enteries: Array(2)}
4: {vouchernumber: "48", vouchertype: "JV", voucherdate: "2020-05-14", enteries: Array(2)}
5: {vouchernumber: "38", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}
6: {vouchernumber: "37", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}
7: {vouchernumber: "33", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(4)}
8: {vouchernumber: "35", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}
9: {vouchernumber: "34", vouchertype: "BV", voucherdate: "2020-05-14", enteries: Array(2)}

如何改进usort函数以获得所需的结果?

评论
  • 抄凝雪
    抄凝雪 回复

    You can try using array_multisort instead of usort:

    array_multisort(
        array_column($voucherdata, 'voucherdate'),
        SORT_DESC,
        array_column($voucherdata, 'vouchernumber'),
        SORT_NATURAL,
        SORT_DESC,
        $voucherdata
    );
    
    var_dump($voucherdata);