Skip to content

Instantly share code, notes, and snippets.

@ahmu83
Created August 3, 2021 16:23
Show Gist options
  • Save ahmu83/23526f44b695e8ea34c1763dfea073da to your computer and use it in GitHub Desktop.
Save ahmu83/23526f44b695e8ea34c1763dfea073da to your computer and use it in GitHub Desktop.
<?php
/**
* Function to check for more than one
* array keys of a long/nested associative array.
*
* i.e, instead of doing this $var['key1']['key2'] (which will result in undefined
* notice if the key/index does not exist) you can do this isset_key($var, 'key1', 'key2')
*
* @param array $var Array variable
* @param string $keys key(s) to check
* @return boolean|string|array
*/
function isset_key($var, ...$keys) {
foreach ($keys as $key) {
if ( is_array($var) && isset($var[$key]) ) {
$var = $var[$key];
} else {
$var = false;
break;
}
}
return $var;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment