donglei2022 2018-08-03 02:26
浏览 34

多数组排序[重复]

This question already has an answer here:

My Array is like this

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

I want to short / re-order main array by sub array's value : [due_date] Please help me. Main array key is not necessary, but sub array keys cannot be changed.

</div>
  • 写回答

1条回答 默认 最新

  • doushi6864 2018-08-03 04:50
    关注

    You need to do the code like this.

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

    This will sort the array according to due date in descending order.But you need to use 'strtotime()' function with date in array.

    评论

报告相同问题?