Call array_key_exists only if $array[$level] is not NULL

If $array[$level] is NULL then array_key_exists will produce:
PHP warnings: array_key_exists() expects parameter 2 to be array, null given in