Use key_exists instead of an index check to prevent undefined index warnings