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