How can the status of a room for a specific date be marked as occupied in PHP, especially when iterating through dates and rooms?
To mark the status of a room for a specific date as occupied in PHP, you can create a multidimensional array where each room is a key and the value is an array of dates that are occupied. When iterating through dates and rooms, you can check if the current date and room combination exists in the array and set the status accordingly.
<?php
// Initialize an array to store occupied dates for each room
$occupiedDates = [];
// Assume $currentDate and $currentRoom are set in the iteration process
$currentDate = '2023-01-15';
$currentRoom = 'Room A';
// Check if the current room exists in the occupiedDates array
if (array_key_exists($currentRoom, $occupiedDates)) {
// Check if the current date is in the list of occupied dates for the room
if (in_array($currentDate, $occupiedDates[$currentRoom])) {
echo "Room $currentRoom is occupied on $currentDate";
} else {
echo "Room $currentRoom is available on $currentDate";
}
} else {
echo "Room $currentRoom is available on $currentDate";
}
// Mark the room as occupied for the current date
if (!in_array($currentDate, $occupiedDates[$currentRoom])) {
$occupiedDates[$currentRoom][] = $currentDate;
}
?>