0

PHP array key exists not working.

array_key_exists doesn’t work with objects implementing Array Access interface. It also ignores possible __get() method in such objects, despite the fact it accepts object as a second parameter.

php array key exists not working

<?php
class DATA implements ArrayAccess {
public $data;
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function __get($property) {
return $this->data[$property];
}
}

$a = new DATA();
$a->data[‘somekey’] = 1;
var_dump(array_key_exists(‘somekey’, $a)); // returns false
var_dump(array_key_exists(‘data’, $a)); // returns true
?>

Code

Changed status to publish
Add a Comment