<?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";
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.4, 8.3.6 - 8.3.13
- array (
0 =>
array (
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24',
),
1 =>
array (
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53',
),
2 =>
array (
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45',
),
)
***
array (
0 =>
array (
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45',
),
1 =>
array (
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53',
),
2 =>
array (
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24',
),
)
***
array (
0 =>
array (
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24',
),
1 =>
array (
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53',
),
2 =>
array (
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45',
),
)
***
array (
0 =>
array (
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45',
),
1 =>
array (
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53',
),
2 =>
array (
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24',
),
)
***
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array (
0 =>
array (
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24',
),
1 =>
array (
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53',
),
2 =>
array (
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45',
),
)
***
array (
0 =>
array (
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45',
),
1 =>
array (
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53',
),
2 =>
array (
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24',
),
)
***
array (
0 =>
array (
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24',
),
1 =>
array (
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53',
),
2 =>
array (
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45',
),
)
***
array (
0 =>
array (
'id' => 2,
'type' => 'comment',
'text' => 'hey',
'datetime' => '2010-05-15 11:29:45',
),
1 =>
array (
'id' => 3,
'type' => 'status',
'text' => 'oi',
'datetime' => '2010-05-26 15:59:53',
),
2 =>
array (
'id' => 4,
'type' => 'status',
'text' => 'yeww',
'datetime' => '2010-05-26 16:04:24',
),
)
***
preferences:
67.89 ms | 414 KiB | 5 Q