代码之家  ›  专栏  ›  技术社区  ›  Mizhar Raja

多数组排序[重复]

  •  -4
  • Mizhar Raja  · 技术社区  · 7 年前

    我的数组是这样的

    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 回复  |  直到 7 年前
        1
  •  0
  •   Magento Dev    7 年前

    你需要这样做。

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

    这将按照截止日期降序排列数组。但您需要在数组中使用带有日期的'strotime()'函数。