-4

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.

Mizhar Raja
  • 174
  • 2
  • 12
  • The truth is: because your comparable dates are `Y-m-d`, you can simply compare them as strings (not dates). You are expected to research and try something before asking here. – mickmackusa Aug 03 '18 at 03:17

1 Answers1

0

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.

Magento Dev
  • 111
  • 9