Skip to content

Instantly share code, notes, and snippets.

@ThierryA
Last active August 17, 2016 21:04
Show Gist options
  • Save ThierryA/f6ff25e66e9e93b7c893 to your computer and use it in GitHub Desktop.
Save ThierryA/f6ff25e66e9e93b7c893 to your computer and use it in GitHub Desktop.
PHP: simply get array values.
<?php
/**
* Get value from $_GET or defined $haystack.
*
* @since 1.0.0
*
* @param string $needle Name of the searched key.
* @param string $haystack Optional. Associative array. If false, $_GET is set to be the $haystack.
* @param mixed $default Optional. Value returned if the searched key isn't found.
*
* @return string Value if found, $default otherwise.
*/
function beans_get( $needle, $haystack = false, $default = null ) {
if ( $haystack === false ) {
$haystack = $_GET;
}
$haystack = (array) $haystack;
if ( isset( $haystack[$needle] ) ) {
return $haystack[$needle];
}
return $default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment