Warning Message From array_key_exists Function in PHP

Warning Message From array_key_exists Function in PHP

The following code will give you PHP Warnings: “Warning: array_key_exists(): The first argument should be either a string or an integer”
// $parts contains an array of values
$publicationId = floor($parts[0]);
if (array_key_exists($publicationId, $this->publisherPublicationMap)) {
    return $this->publisherPublicationMap[$publicationId];
}
The reason being that function “floor” returns value of “float”, which is not the expected value for the key of an array. To fix this, simply cast the value to integer:
$publicationId = (int) floor($parts[0]);
This is not obvious, but very easy fix. Hope this helps.

Leave a Reply

Your email address will not be published.

My new Snowflake Blog is now live. I will not be updating this blog anymore but will continue with new contents in the Snowflake world!