Skip to content

Instantly share code, notes, and snippets.

@mertenvg
Created December 14, 2012 19:48
Show Gist options
  • Save mertenvg/4288086 to your computer and use it in GitHub Desktop.
Save mertenvg/4288086 to your computer and use it in GitHub Desktop.
Extract an option from the results of getopt specifying the keys in order of preference.
/**
* Extract an option from the results of getopt specifying the
* keys in order of preference.
*
* @param array $options Result from getopt('ab:c::', array('add', 'bacon', 'create'))
* @param string $key1 First key to look for e.g. 'add'
* @param string $key2 Second key to look for e.g. 'a'
* @param string $keyn Other possible aliases...
* @return mixed The value from options array or null otherwise
*/
function optval(array &$options, $key1, $key2, $keyn = null)
{
$args = func_get_args();
$options = array_shift($args);
foreach ($args as $key) {
if (isset($options[$key])) {
return $options[$key];
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment