Skip to content

Instantly share code, notes, and snippets.

@danielharan
Created July 1, 2009 00:24
Show Gist options
  • Save danielharan/138521 to your computer and use it in GitHub Desktop.
Save danielharan/138521 to your computer and use it in GitHub Desktop.
# http://svn.facebook.com/svnroot/platform/samples/packages/
# therunaround.tar.gz / core.php
/**
* Returns $arr[$idx], because php doesn't let you index into
* arrays returned from a function.
*
* a()[0] doesn't work
*
* idx(a(), 0) does.
*
* PHP is a pretty stupid language.
*
* @param array to index into
* @param index. if negative, start at the end.
* @param default to return if $arr[$idx] is not set
* @return array[index]
*/
function idx($arr, $idx, $default=null) {
if ($idx === null || !is_array($arr)) {
return $default;
}
$idx = $idx >= 0 ? $idx : count($arr) + $idx;
return array_key_exists($idx, $arr) ? $arr[$idx] : $default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment