Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Array/object isset function, optionally returning a default value
/**
* Check if a property is set for an array/object and returns it. If the property
* doesn't exist, return either a default value, or boolean false.
*
* @param $var array|object The original array.
* @param $checks array An array of properties to be checked.
* @return mixed
*/
function ev_isset( $var, $checks, $default = null ) {
$new_var = null;
if ( ( is_array( $var ) || is_object( $var ) ) && array_key_exists( $checks[0], $var ) ) {
$index = $checks[0];
if ( is_array( $var ) ) {
$new_var = $var[$index];
}
else {
$new_var = $var->$index;
}
}
else {
if ( is_null( $default ) ) {
return false;
}
return $default;
}
if ( count( $checks ) > 1 ) {
array_splice( $checks, 0, 1 );
return _isset( $new_var, $checks, $default );
}
return $new_var;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment