如何排序json数组与日期降序与php



文件夹dataI存储json文件,看起来像这样:

{
"date": "2021-08-05",
"time": "13:00",
"name": "John"
}

循环遍历所有json文件:

$files = glob('data/*.json'); // all json files in data dir
foreach($files as $file) {
$objs = json_decode(file_get_contents($file)); // all json objects in array
echo $objs->date.'<br />';
}

上面的输出给出了所有日期,但以升序排列。我如何按降序输出它们?(最老日期优先)

您可以尝试将所有单独的数组压入单个数组,并使用array_multisort日期排序,如下所示,

$files = glob('data/*.json'); // all json files in data dir
foreach($files as $file) {
$main[] = json_decode(file_get_contents($file),1); // push individual array
}
array_multisort(array_column($main, 'date'), SORT_DESC, $main);
print_r($main);

最新更新