Created
July 1, 2009 00:24
-
-
Save danielharan/138521 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
# 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