1.2.1461. array_key_exists() Works On Arrays

array_key_exists() requires arrays as second argument. Until PHP 7.4, objects were also allowed, yet it is now deprecated.

<?php

// Valid way to check for key
$array = ['a' => 1];
var_dump(array_key_exists('a', $array))


// Deprecated since PHP 7.4
$object = new Stdclass();
$object->a = 1;
var_dump(array_key_exists('a', $object))

?>

See also array_key_exists() with objects and array_key_exists.

1.2.1461.1. Suggestions

  • Use the (array) cast to turn the object into an array

  • Use the native PHP function proprety_exists() or isset() on the property to check them.

1.2.1461.2. Specs

Short name

Php/ArrayKeyExistsWithObjects

Rulesets

All, Analyze, CE, Changed Behavior, CompatibilityPHP74

Exakat since

1.9.0

PHP Version

With PHP 7.4 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

index, array

Available in

Entreprise Edition, Community Edition, Exakat Cloud