Warning: array_key_exists() expects parameter 2 to be array, boolean given
When you encounter the error Warning: array_key_exists() expects parameter 2 to be array, boolean given, it is almost always a symptom of a mismatch in data types rather than a missing key. The function array_key_exists() is strictly typed regarding its second argument; it requires an actual array to search. If you pass a boolean value instead, PHP throws this specific warning because it cannot operate on a true/false scalar value as if it were a collection of keys.
In the specific scenario involving IMAP libraries, this error usually stems from how imap_fetchstructure() behaves when handling messages that lack certain headers or when dealing with empty messages. The function does not always return a populated associative array representing the message structure in every execution path. Depending on the PHP version and the underlying C extension implementation, if the message is empty or malformed, the function might return false (a boolean) instead of an empty array.
When your code executes $structure = imap_fetchstructure($connection, $id, FT_UID);, you are capturing whatever value that function returns. If that message has no parts or specific headers, the function returns false. Consequently, when you reach the conditional check:
if (array_key_exists('parts', $structure)) {
// logic here
}
You are passing that boolean false as the second argument to array_key_exists(). The function immediately fails because it expects an array. It cannot interpret false as a container where keys might exist or not exist.
To resolve this, you must ensure that $structure is treated as an array before attempting to check for specific keys. The most robust approach in PHP 5.x and early 7.x versionsâwhere type declarations were less strictâis to use the is_array() function to validate the input first. You can wrap your logic like this:
$structure = imap_fetchstructure($connection, $id, FT_UID);
if (is_array($structure) && array_key_exists('parts', $structure)) {
// Safely access $structure['parts'] or iterate through them
} else {
// Handle the case where there is no structure or 'parts' key
// This includes cases where imap_fetchstructure returned false
}
This pattern handles both scenarios: when $structure is an empty array (meaning the message exists but has no parts) and when $structure is false (meaning the fetch operation failed to yield a structure object). By checking is_array() first, you prevent the function from receiving a boolean, satisfying its requirement.
In modern PHP 7+ environments, you can enforce this stricter behavior at compile time by declaring variables with type hints. If you are working within a framework like Laravel or building a typed application, defining $structure as an array in your method signature will cause a fatal error if the function returns false, alerting you immediately to the logic flaw rather than allowing the silent failure of the array_key_exists() warning. However, since imap_fetchstructure is part of the IMAP extension and returns dynamic types based on success or failure, you often cannot control its return type directly without explicit validation.
If you are maintaining legacy codebases where strict typing is not enabled, this specific check acts as a necessary guard rail. Ignoring it leads to unpredictable behavior where your script might proceed assuming parts exist when they do not, simply because the boolean was coerced or ignored in loose comparison contexts. Always verify that dynamic data sources from external extensions return the expected structure type before performing key lookups.