You are here

function cf_has_array_key in Common Functionality 7

Same name and namespace in other branches
  1. 7.2 cf.module \cf_has_array_key()

Check to see if the variable is an array and if the given key exists.

Why: According to the PHP documentation: isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does. This means that array_key_exists() should be used to guarantee that a key exists as opposed to isset(). The problem here is that PHP throws an error when the variable is not an array. This means that if (is_array($variable) && array_key_exists($key, $variable)) must be done.

Parameters

string $key: The key to look for.

string $variable: The possible array to search through for the given key.

array $function_history: (optional) An array of function names, ie: array('0' => 'my_function_name').

Return value

TRUE if the array key exists. FALSE if the array key does not exist or a parameter is invalid.

1 call to cf_has_array_key()
cf_node_create in modules/cf_node/cf_node.module
Programatically creates a node of the form type and returns any errors. This flushes the error buffer allowing for multiple calls. This will always return an array.

File

./cf.module, line 156

Code

function cf_has_array_key($key, $variable, array $function_history = array()) {
  cf_error_append_history($function_history, __FUNCTION__);
  if (cf_is_empty_or_non_string($function_history, 'key', $key, WATCHDOG_ERROR)) {
    return FALSE;
  }
  if (is_array($variable)) {
    return array_key_exists($key, $variable);
  }
  return FALSE;
}