Created
August 3, 2021 16:23
-
-
Save ahmu83/23526f44b695e8ea34c1763dfea073da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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