Answer the question
In order to leave comments, you need to log in
How to sort an array of dates and times in ascending order in php?
There are rows in the database with tasks and date/time timestamp ( like this 2021-06-20 14:30:00)
Using foreach I can iterate over the entire array, but I can't find the answer to the question of how to create an array like:
$calendar = [
2021 => [
6 => [
20 => [
1 => [
'name' => 'taskname 1',
'time' => '14:30:00',
],
2 => [
'name' => 'taskname 2',
'time' => '15:00:00',
],
],
],
],
];
Answer the question
In order to leave comments, you need to log in
$array = [
['name' => 'taskname 1', 'date' => '2021-06-20 14:30:00'],
['name' => 'taskname 2', 'date' => '2021-06-21 14:30:00'],
['name' => 'taskname 3', 'date' => '2021-06-20 16:30:00'],
];
$calendar = [];
foreach ($array as $row) {
list($date, $time) = explode(" ", $row['date']);
list($year, $month, $day) = explode("-", $date);
$calendar[$year][$month][$day][] = [$row['name'] , $time];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question