多数组排序

时间:2018-08-03 02:26:43

标签: php arrays sorting multidimensional-array

我的数组就是这样

Array (
[0] => Array (
        [salesID] => 7
        [creditID] => 9
        [amount] => 80400.00
        [due_date] => 2018-12-12
        [status] => no
        [given_date] => 2018-09-30
        [av_class] => table-warning
        [name] => Kumaari
        [contact] => 0
    )

[1] => Array (
        [salesID] => 3
        [creditID] => 8
        [amount] => 500.00
        [due_date] => 2019-06-25
        [status] => yes
        [given_date] => 2018-09-30
        [av_class] => table-success
        [name] => Zayan
        [contact] => 0765894520
    )
 )

我想按子数组的值对主数组进行短路/重新排序:[到期日期] 请帮我。主阵列键不是必需的,但是子阵列键不能更改。

1 个答案:

答案 0 :(得分:-1)

您需要执行以下代码。

<?php
    $inventory = array(
        array(
            "salesID"=>7,
             "creditID"=>9,
             'amount'=>80400.00,
             'due_date'=>strtotime(2018-12-12),
             'status' => 'no',
             'given_date' => 'no',
            'av_class' => 'no',
            'name' => 'no',
            'contact' => 0
            ),
        array(
            "salesID"=>3,
             "creditID"=>8,
             'amount'=>500.00,
             'due_date'=>strtotime(2019-06-25),
             'status' => 'yes',
             'given_date' => '2018-09-30',
            'av_class' => 'table-success',
            'name' => 'Zayan',
            'contact' => '0765894520'
            )

    );
    $date = array();
    foreach ($inventory as $key => $row)
    {
        $date[$key] = $row['due_date'];
    }
    array_multisort($date, SORT_DESC, $inventory);
    echo '<pre>';
    print_r($inventory);
  ?>

这将根据到期日期以降序对数组进行排序。但是您需要对数组中的日期使用'strtotime()'函数。