因此,我试图建立一个多维数组,在其日期下插入日期。但是,这些天仅显示其日期之一来自数据库。它看起来也像在重复自己。最终,我的目标是让所有日期都在他们受人尊敬的日子之下。
我尝试过:-array_push,尝试将新数据推送到数组中。 -A for循环
处理代码
$currentDate = new Date();
$month = $currentDate->getCurrentDate()->format("m");
$year = $currentDate->getCurrentDate()->format("Y");
$availableCollection = new AvailabilitiesCollection();
try {
$availableCollection->add(Availability::getByMonthAndYear($this->db, $month, $year));
$availabilities = $availableCollection->get();
foreach ($availabilities as $this->availability ){
$this->date = new \DateTime($this->availability['date']);
$this->dayLabel = $this->date->format('D');
$this->dataDb[] = $this->dayLabel;
}
$count = array_count_values($this->dataDb);
foreach ($availabilities as $this->availability) {
$this->date = new \DateTime($this->availability['date']);
$this->dayLabel = $this->date->format('D');
$this->insertAvailabilityInDays[$this->dayLabel] = array();
$availableData = array(
'id' => $this->availability['reservation_id'],
'date' => $this->availability['date'],
'start_at' => $this->availability['start_at'],
'end_at' => $this->availability['end_at']
);
array_push($this->insertAvailabilityInDays[$this->dayLabel], $availableData);
// for($i = 0; $i < $count[$this->dayLabel]; $i++){
// $availableData = array(
// 'id' => $this->availability['reservation_id'],
// 'date' => $this->availability['date'],
// 'start_at' => $this->availability['start_at'],
// 'end_at' => $this->availability['end_at']
// );
//
// array_push($this->insertAvailabilityInDays[$this->dayLabel], $availableData);
// }
var_dump($this->insertAvailabilityInDays);
continue;
}
// var_dump($this->insertAvailabilityInDays);
// exit;
} catch (\Exception $e) {
$this->logger->error($e);
}
我的数据库中的数据:
+----------------+------------+----------+--------+
| reservation_id | date | start_at | end_at |
+----------------+------------+----------+--------+
| 6 | 2020-01-22 | 12:30 | 12:45 |
| 7 | 2020-01-20 | 12:50 | 13:00 |
| 8 | 2020-01-22 | 15:45 | 16:00 |
| 9 | 2020-01-21 | 14:45 | 15:00 |
+----------------+------------+----------+--------+
我当前的输出:
array (size=1)
'Wed' =>
array (size=4)
'id' => string '6' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '12:30:00' (length=8)
'end_at' => string '12:45:00' (length=8)
/var/source/app/classes/System/Handlers/CalendarHandler.php:87:
array (size=2)
'Wed' =>
array (size=4)
'id' => string '6' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '12:30:00' (length=8)
'end_at' => string '12:45:00' (length=8)
'Mon' =>
array (size=4)
'id' => string '7' (length=1)
'date' => string '2020-01-20' (length=10)
'start_at' => string '12:50:00' (length=8)
'end_at' => string '13:00:00' (length=8)
/var/source/app/classes/System/Handlers/CalendarHandler.php:87:
array (size=2)
'Wed' =>
array (size=4)
'id' => string '8' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '15:45:00' (length=8)
'end_at' => string '16:00:00' (length=8)
'Mon' =>
array (size=4)
'id' => string '7' (length=1)
'date' => string '2020-01-20' (length=10)
'start_at' => string '12:50:00' (length=8)
'end_at' => string '13:00:00' (length=8)
/var/source/app/classes/System/Handlers/CalendarHandler.php:87:
array (size=3)
'Wed' =>
array (size=4)
'id' => string '8' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '15:45:00' (length=8)
'end_at' => string '16:00:00' (length=8)
'Mon' =>
array (size=4)
'id' => string '7' (length=1)
'date' => string '2020-01-20' (length=10)
'start_at' => string '12:50:00' (length=8)
'end_at' => string '13:00:00' (length=8)
'Tue' =>
array (size=4)
'id' => string '9' (length=1)
'date' => string '2020-01-21' (length=10)
'start_at' => string '14:45:00' (length=8)
'end_at' => string '15:00:00' (length=8)
我希望每个日期都与他们的日子一起出现在数组中。连同其数据库信息。
例如:
'Wed' =>
array (size=4)
0 =>
'id' => string '6' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '12:30:00' (length=8)
'end_at' => string '16:45:00' (length=8)
1 =>
'id' => string '8' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '15:45:00' (length=8)
'end_at' => string '16:00:00' (length=8)
就像提到的@TomShaw,确实更改了$this->insertAvailabilityInDays[$this->dayLabel][] = $availableData
解决了该问题。