<?php
$array = [
[
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45'
],
[
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53'
],
[
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24'
]
];
// usort() by date column DESC:
usort($array, function($a, $b) { return $b['datetime'] <=> $a['datetime']; });
echo var_export($array, true) . "\n***\n";
// usort() by date column ASC:
usort($array, function($a, $b) { return $a['datetime'] <=> $b['datetime']; });
echo var_export($array, true) . "\n***\n";
// usort() by date column ASC in >= PHP7.4:
//usort($array, fn($a, $b) => $a['datetime'] <=> $b['datetime']);
// array_multisort() by date column DESC:
array_multisort(array_column($array, 'datetime'), SORT_DESC, $array);
echo var_export($array, true) . "\n***\n";
// array_multisort() by date column ASC:
array_multisort(array_column($array, 'datetime'), $array);
echo var_export($array, true) . "\n***\n";
preferences:
23.46 ms | 407 KiB | 5 Q