array_key_exists(): Argument #2 ($array) must be of type array, %s given¶
Description¶
Until PHP 8.0, it was possible to use array_key_exists() on both arrays, and objects. The objects were considered as an array, where the properties are the indices, linked to their values.
Since PHP 8.0, it is a deprecated behavior, and in a later version, PHP will make the typing more strict.
The other types, such as integer or string, are not accepted either.
Example¶
<?php
$object = (object) ['a' => 1]; // stdClass
var_dump(array_key_exists('a', $object));
?>
Literal Examples¶
array_key_exists(): Argument #2 ($array) must be of type array, stdClass given
array_key_exists(): Argument #2 ($array) must be of type array, string given
array_key_exists(): Argument #2 ($array) must be of type array, int given
Solutions¶
Cast the object to array before searching.
Call a method on the object to convert it to an array, before searching.
Create a search method, among the properties, on the object.