Skip to content

Instantly share code, notes, and snippets.

@johnabela
Created December 23, 2018 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnabela/e50390380ceebc1dfd76e50f9c26d88a to your computer and use it in GitHub Desktop.
Save johnabela/e50390380ceebc1dfd76e50f9c26d88a to your computer and use it in GitHub Desktop.
/**
* Check to see if an array is suppose to have specific keys.
* This does not check to see if the keys have any values, it
* only checks to see if the keys exists.
*
* @usage
* $array = ['key' => 'meh', 'secret' => 'why'];
* $requiredKeys = ['key', 'secret'];
* array_RequiredKeys($requiredKeys, $array);
*
* @author John B. Abela <github:johnabela>
* @created December 23, 2018 08:25 AM
* @version 1.0.0
* @return null
*/
function array_RequiredKeys($requiredKeys, $array) {
//
if (!is_array($array)) {
die("The array we are suppose to check is not an array.");
}
//
if (!is_array($requiredKeys)) {
die("The requiredKeys array is not an array.");
}
//
foreach ($requiredKeys as $key) {
//
if (!array_key_exists($key, $array)) {
die("Missing a required array key: " . $key);
}
//
if (!is_string($array[$key])) {
die("The array key '" . $key . "' must be a string.");
}
//
}
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment