How can you exclude a specific key in an associative array from a foreach loop in PHP?

To exclude a specific key in an associative array from a foreach loop in PHP, you can use an if statement within the loop to skip over that key. By checking if the current key is the one you want to exclude, you can continue to the next iteration without processing that key.

$array = array("key1" => "value1", "key2" => "value2", "key3" => "value3");

$excludeKey = "key2";

foreach ($array as $key => $value) {
    if ($key == $excludeKey) {
        continue;
    }
    
    echo $key . ": " . $value . "<br>";
}