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

1
  • 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. Commented Aug 3, 2018 at 3:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.