I have the below piece of code which checks the given key exists in the array. But when executing this code I get this error:
"Warning: array_key_exists() expects parameter 2 to be array, boolean given". I am new to PHP and no idea what causes this error.
Code
$structure = imap_fetchstructure($connection, $id, FT_UID);
if (array_key_exists('parts', $structure))
{
};
To protect against someone passing a boolean or null into the function, you can add a simple check to see if
$structureis an array before using it:The simple answer to ‘why’ your original code is broken is that imap_fetchstructure() isn’t finding the requested message and toand returning a
false,null, or0. The documentation http://php.net/manual/en/function.imap-fetchstructure.php doesn’t indicate what’s returned on failure, but it’s easy to guess. Most php functions that return objects but are unable to complete return a null or false on failure (when I say failure I don’t mean error or an exception, just couldn’t do or find whatever you asked of it).