我有一个列出日期时间的数组。如何对具有相同日期的数组进行分组。
$result = [
0 => [
"2019-07-17 10:02:00",
"2019-07-17 12:00:00"
],
1 => [
"2019-07-18 08:00:00"
],
2 => [
"2019-07-19 01:00:00",
"2019-07-19 02:00:00"
]
];
预期的结果:
{{1}}
答案 0 :(得分:2)
尝试一下:
$grouped = [];
foreach ($array as $dateString) {
$dateObject = new \DateTime($dateString);
$grouped[$dateObject->format('Y-m-d')][] = $dateString;
}
$grouped = array_values($grouped);
var_dump($grouped);