How can arrays be effectively utilized in PHP to store and retrieve time-based text messages?

To store and retrieve time-based text messages in PHP using arrays, you can create an associative array where the keys represent timestamps and the values represent the corresponding messages. This allows you to easily store and retrieve messages based on their associated time.

// Create an associative array to store time-based text messages
$timeBasedMessages = [
    strtotime('2022-01-01 10:00:00') => 'Message 1',
    strtotime('2022-01-02 15:30:00') => 'Message 2',
    strtotime('2022-01-03 08:45:00') => 'Message 3'
];

// Get the current timestamp
$currentTimestamp = time();

// Iterate through the array to find the message for the current time
foreach ($timeBasedMessages as $timestamp => $message) {
    if ($currentTimestamp >= $timestamp) {
        echo $message;
        break;
    }
}